TEMPE, Ariz. -- Backup quarterback Bryson Beirne, on the only pass he threw all night, connected with Juron Criner on a 23-yard touchdown play with 5:18 remaining and Arizona held on for a 31-27 Territorial Cup victory over Arizona State on Saturday night.

The game ended with the Sun Devils (6-5, 4-4) on the Arizona 15-yard line, where two passes by Brock Osweiler fell incomplete.

Beirne had just come on in relief of game MVP Nick Foles, who completed 35 of 51 for 370 yards and two touchdowns before being shaken up and leaving the game. Criner caught nine passes for 134 yards for Arizona (3-8, 2-8).

Osweiler was 36 of 65 for a career-best 487 yards and a touchdown but was intercepted twice. The Sun Devils' Gerell Robinson had 11 receptions for 199 yards, both career bests.

Arizona State's third loss in a row does not eliminate the Sun Devils from a shot at representing the Pac-12 South in the conference's first championship game. The Sun Devils trail UCLA by a game and the Bruins hold the tiebreaker with one game to play.

Arizona State must beat California next week and needs UCLA to lose to Southern California and Utah to beat Colorado. That would create a three-way tie between ASU, UCLA and Utah, with the Sun Devils getting the nod because they would have the best record within the division.

Foles broke Arizona single-season records for yards passing and pass completions and still has a game to play, a nonconference contest at home against Louisiana-Lafayette.

After Arizona jumped ahead 14-0 early, Arizona State scored the next three touchdowns to pull ahead.

Osweiler had passes of 17 and 34 yards to Robinson on the ensuing drive, with Marshall scoring on a 1-yard run to cut it to 14-7 with two minutes to go in the first quarter. John Bonano's 49-yard field goal attempt on Arizona's next possession bounced off the left upright and was no good. The Sun Devils took over at their 32 and went 48 yards in 11 plays. Robinson was wide open in the end zone on an 11-yard pass for the score and it was tied at 14-14.

Osweiler's 58-yard pass to Rashad Ross, followed by Marshall's 22-yard burst untouched up the middle for the touchdown put Arizona State ahead 21-14 with 4:29 left in the half. Bonano missed a 29-yard field goal barely to the right, but hit 43-yarder with two seconds left to cut Arizona State's lead to 21-17 at the half. Alex Garoutte's 23-yard field goal provided the only points of the third quarter and put the Sun Devils up 24-17 entering the fourth.

Foles was 6 of 7 for 79 yards on a 7-play, 94-yard drive that cut the lead to 24-21. Foles threw short to Gino Crump, who evaded four would-be tacklers on a 33-yard touchdown play with 10:25 remaining.

Foles completed 6 of 6 passes for 67 yards on a 10-play, 80-yard scoring drive to start the game. The big play was a 48-yard pass to Criner. The Sun Devils stuffed the Wildcats for five yards in losses on the next two plays, but a pass interference penalty moved it to the 1, where Keola Antolin bulled in for the touchdown.

Arizona State, on the subsequent possession, drove to the Arizona 29, but Shaquille Richardson picked off Osweiler's pass and returned it 42 yards to the Sun Devils 49. Foles threw short to Antolin, who darted through the defense 46 yards to the Arizona State 13. Three plays later, Foles threw 11 yards to Ka'Deem Carey for the touchdown and it was 14-0 with 5:34 left in the first quarter.

Robinson became the first Arizona State player to top 1,000 yards receiving in a season since Derek Hagan in 2005. He topped 100 yards receiving for the fifth time in six games.