SEATTLE — Washington had three touchdowns before Stanford managed a first down Saturday night.

Even the most die-hard Pac-12 fans watching on TV probably turned to Alabama-LSU.

But quarterback K.J. Costello and his receivers — minus injured JJ Arcega-Whiteside — made it a game before the Huskies hung on for a 27-23 win.

“When things are tough and dark and it’s us against the world, we are at our absolute best,” Cardinal head coach David Shaw said. “I love that about our guys. The problem is we create the situation that puts us there.

“We’ve got to find a way to stop screwing it up to get to the point where we have to be at our best just to get back in the game.”

Arcega-Whiteside, who entered the weekend second in the nation in touchdown catches with 11, injured his right knee in the second quarter and didn’t return.

His absence hurt the offense “very significantly,” Shaw said. “JJ’s an All-American. He’s one of the best, if not the best, receiver in America. When a guy like that’s not on the field, you miss him.”

Costello threw a 33-yard touchdown pass to Trent Irwin with 3:24 left, but Collin Riccitelli — filling in for an injured Jet Toner — missed the extra point. That made it 27-23.

Stanford, out of timeouts, took over after a Washington punt with 2:16 left. Costello passed the Cardinal the Washington 34, but his Hail Mary pass for Kaden Smith in the end zone was picked off by Taylor Rapp on the final play.

The loss, Stanford’s fourth in its past five games, dropped the Cardinal to 5-4, 3-3 in the Pac-12.

Costello completed 23 of 43 passes for 347 yards and two touchdowns, but he also threw three interceptions. Smith had eight catches for 107 yards.

“We need to come out strong, faster, on both sides of the ball,” Smith said. “I’m proud of my team for fighting through all the adversity that we faced.”

Washington’s Jake Browning was 16-for-27 passing for 194 yards and a touchdown. He also eluded tacklers repeatedly on scrambles. Myles Gaskin, who missed the previous two games with a shoulder injury, rushed for 148 yards for the Huskies (6-4, 4-3).

“It’s nice to have No. 9 back,” Huskies head coach Chris Petersen said. “He’s a game-changer.”

Through intermittent rain at Husky Stadium, 69,690 fans thoroughly enjoyed their team’s first-half superiority.

But Costello put Stanford on the board with a 14-yard touchdown pass to Smith in the third quarter.

“At Stanford I know they don’t lose back-to-back games, let alone three or four times in a five-game stretch,” Costello said. “This one really hit me that we’ve had this feeling a lot the past month. It’s a very similar feeling, and it’s a feeling no one should have. I don’t wish it upon anybody.”

Washington had two field goals in the second half from Peyton Henry, a Danville native.

Stanford’s Bryce Love, who started the game after being questionable with a sore ankle, had 71 yards in 18 carries. He scored on a 5-yard run to cut the lead to 24-14 in the third quarter.

The play followed a 15-yard personal foul penalty against Rapp for a horse-collar tackle on Love, a call that drew a chorus of boos.

A 22-yard field goal by Riccitelli made it 24-17 early in the fourth quarter before Henry’s second field goal put the Huskies up 27-17.

Browning, who had been benched briefly a week earlier in a 12-10 loss at Cal, ran for a touchdown and threw for another to tight end Drew Sample in the first quarter. Gaskin added another score on a 6-yard run, and it was 21-0 early in the second quarter.

Costello threw two interceptions in the first half. The Huskies’ first touchdown followed an interception by Byron Murphy on a pass intended for Irwin.

Another pass, on a screen play intended for Love, was tipped by Menlo Park native Ben Burr-Kirven and intercepted by lineman Greg Gaines. If it hadn’t been tipped, Shaw thought Love might scored on it.

“There was absolutely no panic at halftime because we knew we hadn’t played well,” Shaw said.

Burr-Kirven had 12 tackles for the Huskies and Bobby Okereke had 11 for the Cardinal.

Tom FitzGerald is a San Francisco Chronicle staff writer. Email: tfitzgerald@sfchronicle.com Twitter: @tomgfitzgerald