Updated

PASADENA, Calif. -- Gabe Marks made a diving 21-yard touchdown reception with 3 seconds to play, sending Washington State to a jaw-dropping 31-27 victory over No. 19 UCLA on Saturday night.

Josh Rosen scrambled 37 yards and dived for the go-ahead touchdown with 1:09 to play for the Bruins (7-3, 4-3 Pac-12), capping a desperate 80-yard drive.

Luke Falk answered for Washington State (7-3, 5-2) with his own spectacular 75-yard drive ending with a remarkable, physical catch by Marks, who became the Cougars' career receptions leader earlier in the game with his 196th catch.

Falk returned from a mid-game injury after passing a concussion protocol to throw for 331 yards. He threw two TD passes to Marks, and the Cougars punctuated their resurgent season with their first win over a ranked team in 10 tries since 2013.

Coach Mike Leach's club has won five of its last six games, giving the Cougars seven victories for the first time since 2003.

"Great job by our team being resilient," Falk said. "We've done it all year, and did it again tonight."

Rosen went 33 for 57 for 340 yards, and the precocious freshman appeared to clinch the win after Jaleel Wadood's end-zone interception with 3:13 left. Rosen sprinted through the defense for his score and subsequently hit Thomas Duarte for the 2-point conversion.

But the Cougars went 75 yards in 66 seconds, and Marks hung on to the ball despite contact with the UCLA defender. The Bruins were whistled for pass interference on the play.

UCLA lost after leading at halftime for the first time in 32 games under coach Jim Mora. The Bruins had won their last three games after a two-game skid and also lost despite holding Washington State's prolific Air Raid offense to 288 yards in the first three quarters.

Nate Starks had an early TD run for the Bruins, who still control their fate in the Pac-12 South despite a mediocre home finale. UCLA would win the division with season-ending road victories over Utah and Southern California.

Although Washington State would still need ample help to contend for the Pac-12 North title, Stanford's loss to Oregon gave the Cougars reason to hope their comeback season could get even better.

Falk returned to the game in the second half after taking a violent sack and leaving the field in the second quarter. Falk, the Pac-12's leader in yards passing and total offense, appeared to hit his head on the ground while getting hit by Jacob Tuioti-Mariner.

Freshman Peyton Bender threw his first career touchdown pass to Dom Williams during Falk's absence.

The Bruins allowed Washington State to stay close by hurting themselves: Ishmael Adams fumbled a punt return at the UCLA 14 in the second quarter and then committed defensive holding while his teammates sacked Bender on third down, allowing Bender to hit Williams for a short TD and a 14-13 lead on the extra snap.

UCLA kicker Ka'imi Fairbairn became the leading scorer in Pac-12 history with his 392nd career point when the Bruins' opening drive ended with a 22-yard field goal. UCLA center Jake Brendel set a school record on the same drive with his 49th career start, surpassing linebacker Spencer Havner's 10-year-old mark.

Marks' TD catch midway through the third quarter put Washington State up 21-16. Marks broke Michael Bumpus' school receptions record early in the second half.