AMES, Iowa (AP) — Iowa State quarterback Joel Lanning thought he saw a touchdown catch. So did ISU coach Paul Rhoads and more than 50,000 fans at Jack Trice Stadium.

But the replay booth thought otherwise.

Receiver Trever Ryen trapped the ball, it was ruled, and Iowa State settled for a 21-yard Cole Netten field goal, a four-point difference in a game the Cyclones lost 35-31 to No. 5 Oklahoma State on Saturday.

"From my view, it looked like he had the ball," Lanning said of his 3-yard first-quarter toss. "I didn't think it hit the ground. If we get that, we're tied at 35-35 and we're in overtime now trying to win the game."

But the Cyclones had to live with call and Oklahoma State rallied for a victory that kept the Cowboys unbeaten, backup quarterback J.W. Walsh throwing a 7-yard pass to Jeff Carr for the winning touchdown with 3:06 left.

Oklahoma State (10-0, 7-0 Big 12) trailed 24-7 and 31-21 before pulling out a victory that might not look impressive but was still critical in jockeying for one of the four spots in the College Football Playoff.

The Cyclones (3-7, 2-5) were left wondering what might have been if they had just made a couple of more plays.

"Plenty of fight in our kids, and they showed it against a team ranked fifth in the country and now sits 10-0 and has an opportunity to finish up in the playoffs and play for a national championship if they keep executing and winning," ISU coach Paul Rhoads said.

Walsh drew the Cowboys to 31-28 with a 16-yard touchdown run early in the fourth quarter and OSU drove 84 yards in 11 plays for the winning score after forcing a punt.

Starter Mason Rudolph got Oklahoma State to the Iowa State 7 and Walsh, usually the team's running quarterback, took it from there. He hit Carr with a swing pass on the left side and two blocks sprung the freshman to the end zone.

"You don't win without having some maturity and a relentless football team," Walsh said, "and we've been able to attain those things very well. There was not a doubt in anyone's mind. You could see it in everybody's eyes."

Lanning had helped keep Iowa State ahead for most of the game, running for two touchdowns and passing for another. He and the Cyclones had two chances after Carr's score, but gave up the ball on downs and then turned it over with an interception after getting it back.

The two teams met in a setting similar to 2011, when an unheralded Iowa State team ended No. 2 Oklahoma State's BCS championship hopes with a victory in two overtimes. On a pleasant, late autumn day, the Cowboys survived in this one, though for the longest time, things were looking grim for coach Mike Gundy's team, mostly because of Lanning.

The sophomore from nearby Ankeny consistently ran for nice gains on read options and scrambles and did just enough with his passing to keep the Cyclones moving.

He carried 14 times for 130 yards and scored on runs of 24 and 21 yards. The 21-yarder, on which he ran untouched to the end zone after faking a handoff, gave Iowa State a 31-21 lead late in the third quarter and fueled hopes of another upset.

It just wasn't quite enough, in part because Oklahoma State controlled the ball with long, time-consuming scoring drives in the second half.

"In my mind, I thought two touchdowns in the second half would win the game," Rhoads said. "We got the one. We just couldn't find a way to get the second."

Lanning also completed 11 of 22 passes for 162 yards, including a 24-yarder to Allen Lazard for a TD in the first half, when Iowa State scored three touchdowns and kicked a field goal on its first four possessions.