CORVALLIS, Ore. (AP) — Oregon State brought a six-game winning streak and a lot of questions into Saturday night's game against No. 8 Stanford.

Primarily, how would the Beavers fare in the season's first game against a top 25 opponent?

Oregon State didn't get the ultimate result, thought it took the Cardinal to the final seconds before falling short in a 20-12 loss.

The Beavers (6-2, 4-1 Pac-12) trailed 20-9 with late, yet nearly forced overtime. After Trevor Romaine kicked a 39-yard field goal with 3:57 left to cut the deficit to eight, Oregon State drove to the Stanford 7 with 30 seconds remaining. However, four consecutive incompletions ended the rally.

"We played a great team, and we lost by eight points. Stanford's a top 10 team. We're going to learn from this," Beavers receiver Brandin Cooks said.

Tyler Gaffney ran for three touchdowns and 145 yards to lead Stanford (7-1, 5-1). Gaffney accounted for all of the Cardinal touchdowns on runs of 4, 9 and 32 yards.

Stanford totaled 276 yards on offense, as quarterback Kevin Hogan completed just 8 of 18 passes for 88 yards.

The Cardinal held Oregon State to 288 yards, well off the Beavers' season average of 515 yards. Beavers quarterback Sean Mannion, the national leader in yards passing and touchdown passes, was 41 of 57 for 271 yards and one touchdown.

Oregon State coach Mike Riley called the outcome "a really tough loss, and it will be even tougher for our team tomorrow, for our team and coaches watching the film. There were a ton of missed opportunities ... but I'm thankful for our team. I just love how they fought."

Stanford's defense came up big in key situations, as it turned back Oregon State on downs four times inside the Cardinal 35. Stanford held Mannion to fewer than 350 yards passing for the first time this season, and sacked him eight times.

Mannion felt Oregon State "left some plays out there that might have been the difference," but disagreed that the Beavers shot themselves in the foot.

"By saying that, it takes away from Stanford, and they definitely earned this win," Mannion said.

The Cardinal win sets the stage for a showdown against No. 2 Oregon at home on Nov. 7. Last year, Stanford handed the Ducks their only loss of the season with a 17-13 win in Eugene.

Stanford led 7-3 at halftime following a defensive struggle in the first half.

Oregon State took a 3-0 lead early in the second quarter on Romaine's career-best 50-yard field goal. The Beavers appeared they would lead hold the lead into the break when a turnover on downs gave the Cardinal the ball on its own 34 with 46 seconds remaining in the second quarter.

Stanford took advantage with a quick-striking drive, finishing when Gaffney bulled into the end zone from 4 yards out 7 seconds before halftime.

The Cardinal capitalized on their momentum by forcing a fumble on the opening kickoff of the second half. Gaffney scored two plays later on a 9-yard run to put Stanford ahead 13-3.

Oregon State bounced back with its best possession of the night, a 12-play, 90-yard drive ending on an 8-yard touchdown pass from Mannion to Cooks. But the Cardinal made it 20-9 just 3 minutes into the fourth quarter on Gaffney's 32-yard cutback run for a touchdown.

NOTES: Terron Ward's 34-yard run midway through the third quarter was Oregon State's longest running play of the season. ... Romaine missed his first extra-point kick in 37 tries this season.