Updated

Stanford coach David Shaw waited at midfield for several minutes, but couldn't find Ed Orgeron amid the raucous crowd of Southern California fans celebrating their Trojans' upset victory.

The fifth-ranked Cardinal's national title hopes are also lost, and Shaw figured they had only themselves to blame.

Andre Heidari kicked a 47-yard field goal with 19 seconds to play, and a stalwart defense repeatedly came up big in USC's 20-17 upset of Stanford on Saturday night.

USC's remarkable defensive performance included two fourth-quarter interceptions and a season-low 17 points from the Cardinal (8-2, 6-2), who followed up last week's win over Oregon by getting knocked out of the BCS title chase and maybe the Rose Bowl race as well.

Shaw knew the loss didn't rest with his defense, which shut out USC for 38 minutes before Heidari's kick.

"We didn't make enough plays offensively," Shaw said. "When we did make plays, they didn't result in points. Against a good team like this, playing at home, you have to get points. We went down there twice (in the second half) and had a field goal blocked and threw an interception. That's not going to win on the road in this conference."

Tyler Gaffney rushed for 158 yards and two touchdowns for Stanford, which had won three straight while closing in on the chance to host the Pac-12 title game for the second straight year. Kevin Hogan went 14 of 25 for 127 yards, but the Cardinal couldn't regain the lead after trailing 17-10 at half, despite repeatedly getting close.

"I think this is the first time anyone on this team has lost to them," Gaffney said. "It's a tough thing to swallow."

Indeed, Stanford had won in its last three trips to the Coliseum, including a triple-overtime thriller two years ago, and its last four meetings with USC overall.

Stanford tied it with Gaffney's 18-yard TD run on the first drive of the third quarter, but couldn't score on six drives in the final 23 minutes, managing just 88 yards — 75 on a single drive ending in Dion Bailey's interception at the USC 6.

Cody Kessler passed for 288 yards as the revitalized Trojans (8-3, 5-2 Pac-12) earned their fifth win in six games under the irrepressible Orgeron, who replaced the fired Lane Kiffin in late September. Orgeron immediately returned fun to a dour program — and wins have followed, none bigger than this streak-snapping victory over powerful Stanford.

"In our tight-knit family, there was a belief that we were going to find a way to win the game," Orgeron said. "I'm proud of our young men. This is something that they've wanted for a long time now."

Soma Vainuku caught an early TD pass and Javorius Allen rushed for a score for USC. Nelson Agholor had eight catches for 104 yards, while Marqise Lee had six catches for 83 yards — and both receivers made big plays on USC's final drive.

"I don't know if I pulled them together," Orgeron said. "I think they just came together. The leadership of this program is really coming to the forefront."

USC's offense also struggled throughout the second half, but the Trojans' sanctions-depleted defense kept it close despite using just two substitutes for most of the night.

Bailey intercepted Hogan's third-down pass with about 10½ minutes to play, killing what seemed certain to be Stanford's go-ahead drive. Su'a Cravens then intercepted a tipped pass at the USC 44 with 3 minutes to play, giving another chance to the Trojans' offense.

After Lee caught a 13-yard slant pass over midfield on fourth-and-2, the Trojans reached the Stanford 21 before two straight yardage-losing plays pushed them back to the 30 — and Heidari still nailed his kick.

"This is definitely the most electrifying game I've been a part of in the four years I've been here," Bailey said.

Stanford betrayed a few nerves early. The Cardinal burned two timeouts and committed an illegal-snap penalty on the game's first drive, and Ty Montgomery dropped two passes in the opening minutes.

USC's opening drive was smooth, with Lee making a tiptoe sideline catch before Vainuku came out of the backfield for a play-action 1-yard TD catch just 6:13 in.

Gaffney scored on a 35-yard run moments later when he popped out the back of a scrimmage pile and sprinted past the slumbering USC defense, but the Trojans answered with another long drive capped by Allen's 1-yard TD run and a 2-point conversion catch by Lee.

Kessler was outstanding in the pocket and on the run, completing 10 straight early passes before Heidari's first field goal put the Trojans up by 10 points early in the second quarter. Stanford added a field goal with 17 seconds left in the first half.

After Gaffney capped a 92-yard drive after halftime with his TD run up the middle, Stanford forced a fumble by Kessler at the USC 19 — but the Cardinal's field goal attempt was blocked.

Former Stanford lineman Jonathan Martin watched the game from the sideline.