Heisman Trophy contender Marcus Mariota was inaccurate and under pressure much of the night, but he threw two fourth-quarter touchdown passes, sandwiched around a blocked field goal return for a score by Rodney Hardrick, to pull the Ducks (8-1, 5-1) to 26-20 with 2:12 left.
Oregon couldn't recover a second onside kick and Stanford ran out the clock.
And the biggest winner of all? No. 3 Florida State. The Seminoles don't have to worry about the Ducks nudging them out of second place in the BCS standings.
FSU was in danger of slipping to third in the BCS if Oregon could have registered a big road victory against a quality opponent. Now, the Seminoles face a manageable remaining schedule with a good chance to win their way to the BCS championship game at the Rose Bowl in January.
Unbeaten Baylor and Ohio State have to be happy, too, with one less hurdle to clear.
As for Stanford, it gets first place in the Pac-12 North and the inside track to another league title game. And maybe if things get really weird, the Cardinal can get back in the title hunt.
Stanford won a three-point game in overtime at Oregon last year to deny the Ducks a chance to play for the national title, but it didn't look as if there would be much drama in the return bout on the Farm. Stanford led 17-0 at halftime and added three more field goals by Josh Williamson in the second half.
Oregon looked like dead Ducks, down 26-0 early in the fourth with Stanford hammering away behind Gaffney, who set a school record with 45 carries. Even after Oregon finally broke the seal with a 23-yard touchdown pass from Mariota to Daryle Hawkins, the Cardinal went on another time consuming drive and attempted a long field goal that would have sealed it.
Instead, the Ducks blocked it, Hardrick scooped and scored from 65 yards out with 5:08 left and suddenly it was interesting.
Only 5 seconds later it got even more interesting when the Ducks recovered an onside kick. They quickly moved inside the Stanford 5, but got pushed back to a fourth-and-goal from the 12. Mariota threw a touchdown pass to Pharoah Brown with 2:12 remaining, but the time it took the Ducks to get in while burning a timeout was key.