Stanford head coach David Shaw could sense it as early as the team’s walk-through Saturday morning.

His quarterback was coming back and permeating the entire team with energy and confidence.

“I can’t say enough about K.J. Costello,” Shaw said. “It wasn’t just throwing the football. … You felt his energy, felt his passion. He was pushing guys, talking to them, giving us great feedback coming off the field. ... I can’t say enough about the juice that he gave us as a player and as a captain and leader for us.”

In his first game back after missing more than a month with a thumb injury, Costello threw for 312 yards and three scores and ignited Stanford to a 41-31 victory over Arizona on sun-kissed Saturday afternoon in front of a homecoming crowd of 31,711 on The Farm.

It was a resounding response from the Cardinal (4-4, 3-3 Pac-12), who managed just 198 yards of total offense on 3.2 yards per play last weekend in a 34-16 home loss to UCLA and allowed the Bruins to rack up 263 rushing yards on 6.1 yards per carry.

Schedule Date Opponent Time Aug. 31 N’western W , 17-7 Sept. 7 at USC L, 45-20 Sept. 14 at Cen. Fla. L, 45-27 Sept. 21 Oregon L, 21-6 Sept. 28 at Ore. St. W, 31-28 Oct. 5 Wash. W, 23-13 Oct. 17 UCLA L, 34-16 Oct. 26 Arizona W, 41-31 Nov. 9 at Colorado TBD Nov. 16 at Wash. St. TBD Nov. 23 Cal TBD Nov. 30 Notre Dame TBD

Read More

With Costello returning to run the offense and vociferously demanding that the defense get him the ball back as quickly as possible, the Wildcats (4-4, 2-3) couldn’t stop Stanford in the air or on the ground.

Costello went 30-for-43, completing passes to 12 receivers. Connor Wedington had a team-best seven catches for 61 yards, and Simi Fehoko had a team-high 97 receiving yards and two touchdowns. Cameron Scarlett ran 19 times for 102 yards (5.4 yards per carry) and two bruising touchdowns.

“It seems like it’s been an eternity, man,” Costello said of what it felt like to take his first snap since Sept. 21. “… You play this game, because you want to do anything for your boys. When you feel like you can only be a voice on the sideline, that’s not what I signed up to do.

“I’ll challenge myself to play with that energy for the rest of my career. … That’s the expectation. That’s the standard. I try to bring that every week.”

That energy bled into the defense, which got a combined 15 tackles from Andrew Pryts and Stuart Head and two fourth-quarter interceptions from Paulson Adebo. After allowing 24 points on 298 yards in the first half, Stanford yielded just seven points on 197 yards in the second.

The Cardinal took an 11-point lead on a 44-yard touchdown pass from Costello to Fehoko with 4:36 remaining in the first half, but about 5½ minutes into the third quarter, Arizona tied it 31-31 on a 9-yard pass from Khalil Tate to Drew Dixon.

That would have been an easy time for Stanford to fall into the same traps it had during a 1-3 start to the season, but Costello wouldn’t allow it. He worked too hard to get back on the field.

The senior hooked up with Fehoko on a 14-yard touchdown with 5:11 remaining in the third quarter and drove the team 60 yards for the game-clinching 21-yard field with 6:18 on the fourth-quarter clock.

Costello tried to play a week earlier, but his thumb was still too sore to grip the ball. Shaw had his fingers crossed that the injury wouldn’t get angered after each practice this week, and after pregame warm-ups, it was clear that his quarterback was going to gut it out.

“Make no mistake about it. He’s in pain,” Shaw said. “It’s not easy. He’s not 100%, but he can go out there and throw it. …

“He went out there and played one of the better games of his career. I’m really, really proud of him.”

Rusty Simmons is a San Francisco Chronicle staff writer. Email: rsimmons@sfchronicle.com Twitter: @Rusty_SFChron