The game was delayed 23 minutes because of a power outage with 3:58 left in regulation and the Aztecs behind 17-13.
Before that delay, San Diego State (3-0) was driving at the Stanford 42. The Aztecs kept going when play resumed.
Christian Chapman completed three consecutive passes after the lights came back on, culminating with an 8-yard touchdown pass to tight end David Wells with 54 seconds left, putting the Aztecs ahead 20-17.
On Stanford's ensuing possession, Cardinal quarterback Keller Chryst was intercepted by cornerback Kameron Kelly to secure the historic upset, dropping Stanford's record to 1-2.
San Diego State achieved its third consecutive victory over a Power 5 opponent -- all from the Pac-12 -- for the first time in program history. The Aztecs won at Arizona State last week and defeated California last season in San Diego.
Stanford running back Bryce Love conjured images of former Heisman Trophy candidate Christian McCaffrey, leading the Cardinal with a career-high 184 yards rushing.
San Diego State's Rashaad Penny, producing like former Aztecs running back Donnel Pumphrey, ran for 175 yards on 32 carries with one touchdown.
Love amassed his yards on only 13 carries, producing at an average of 14.2 yards per carry. He scored on touchdown runs of 51 and 53 yards.
Love's 53-yard score on the first play of the fourth quarter put Stanford ahead for the first time since the Cardinal led 7-3 on Love's 51-yard sprint with 12:38 left in the second quarter.
After Love's score in the fourth quarter, Penny answered with a 36-yard run to the Stanford 20.
The drive stalled when Stanford linebacker Casey Toohill sacked Chapman on a third-and-14 play, forcing John Baron II to attempt a 50-yard field that he missed, breaking his streak of six consecutive conversions to start the season.
Baron made a 36-yard field with 8:33 left in the third quarter to give San Diego State a 13-7 lead. It was Baron's FBS-record 25th consecutive conversion within 50 yards.
Stanford did not convert on a third-down attempt in six tries until Love rushed six yards to the Stanford 40 with 7:45 left in the third quarter. Love followed with a 47-yard run to the San Diego State 13.
The Cardinal drive ended at the 4 with coach David Shaw electing to attempt a field goal instead of going for the first down on fourth-and-inches. Jet Toner's 20-yard field goal with 4:51 left in the third quarter cut San Diego State's lead to 13-10.
Stanford's first turnover in its last six games came at a poor time for the Cardinal. Chryst's pass was intercepted by cornerback Ron Smith, who returned the ball 22 yards to Stanford's 15 with 1:14 left in the first half.
Penny, coming off a 216-yard performance at Arizona State last week, eventually scored on a 4-yard run with 43 seconds left in the half to put the Aztecs up 10-7.
Chryst then fumbled the ball away with 19 seconds left in the half after driving the Cardinal to the San Diego State 25. The ball was jarred loose on a sack by Kelly and was recovered by defensive tackle Damon Moore.
Neither offense got on track for most of the first half.
Stanford did not get a first down in the first quarter. The Cardinal had more penalty yards (10) than total yards (three) in three possessions entering the second quarter.
Stanford's initial first down of the game occurred with 13:13 left in the second quarter on Chryst's 11-yard pass to Trent Irwin to the Cardinal 49.
Love followed with his 51-yard touchdown run that enabled the Cardinal to take a 7-3 lead with 12:38 left before halftime.
Stanford finished the half with only four first downs and was 0 for 6 on third-down conversions. The Cardinal had 85 total yards in the first half while San Diego State had only 69.
Chryst finished the game 9 of 20 for just 72 yards, no touchdowns and two interceptions. Chapman was 21 of 29 for 187 yards, one score and no interceptions.