Running back Ronald Jones II provided the exclamation mark with a 23-yard touchdown rush with 4:15 left in the fourth quarter. It was Jones' second score of the night, and the Trojans snapped a three-game losing streak to the Cardinal.
The win was also just the second for USC over Stanford at Los Angeles Memorial Coliseum in the last decade. Stanford won four of the previous five here since 2007.
Jones and freshman Stephen Carr both surpassed 100 yards rushing, with Jones winding up with 116 yards and Carr getting 119.
USC quarterback Sam Darnold completed 21 of 26 pass attempts for 316 yards and four touchdowns.
USC maintained a double-digit lead throughout the second half, conceding just a single score after intermission. That came on a 3-yard pass from quarterback Keller Chryst to wide receiver J.J. Arcega-Whiteside with 6:41 remaining.
Linebacker Cameron Smith paced the USC defense with eight tackles, playing his first full game of the season. He missed the first half Week 1 serving a half-game suspension due to a targeting penalty received in January's Rose Bowl Game.
The efforts of Smith and the USC front seven after halftime held in check a Stanford rushing attack that gashed the Trojans through the first two quarters. Cardinal running back Bryce Love gained 141 of his 160 yards before intermission.
The Trojans threatened to blow the game open in the third quarter, but a promising drive ended short of the end zone on a spectacular interception by cornerback Alijah Holder. Darnold made few errors on the night, save two interceptions on passes thrown into traffic.
USC made spectacular plays of its own, including a one-handed grab by wide receiver Deontay Burnett on a third-and-6 in the fourth quarter. His reception -- one of nine for 142 yards -- helped ice the Trojans' win by turning a potential punting situation into their first scoring drive of the second half.
Darnold capped the possession with his second touchdown pass to wide receiver Steven Mitchell, who finished with 94 yards on four catches.
Burnett also reached the end zone twice.
USC scored on four of its five possessions to take a 28-17 halftime lead.
Darnold completed his first 11 pass attempts, including two for touchdowns. He went into intermission 14 of 17 for 225 yards with three touchdowns.
Burnett was Darnold's top target, catching six passes for 73 and two touchdowns before halftime. He dove to make both scoring receptions -- a 22-yarder into the corner of the end zone early in the second quarter and a 25-yarder just before halftime.
Darnold's first incompletion preceded an interception. Stanford safety Justin Reid leaped to catch a throw made under pressure, setting up the Cardinal near midfield. The ensuing possession resulted in a 36-yard field goal by Jet Toner.
Stanford's previous two scoring drives forced ties. The Cardinal answered a 1-yard Jones touchdown rush when Chryst found tight end Dalton Schultz for a 1-yard score. Chryst ended the half 9 of 14 for 81 yards with completions to six different targets.
For the game Chryst was 15 of 28 for 172 yards, two touchdowns and no interceptions.
Darnold capped a game-opening, 5-of-5 drive with a 4-yard touchdown strike to wide receiver Steven Mitchell in the back of the end zone. Mitchell led all receivers with 83 yards at the half.
Stanford responded when Love took a pitch on the left side and broke 75 yards for the touchdown.