ORCHARD PARK, N.Y. (AP) — Josh Allen threw a 42-yard touchdown pass to Robert Foster early in the fourth quarter Sunday to rally the Buffalo Bills to a 14-13 win over Detroit that eliminated the Lions from playoff contention.

Allen also scored on a 3-yard run and finished 13 of 26 for 204 yards passing and no turnovers. Foster had four catches for 108 yards to top 100 for the third time in five games since being promoted from the practice squad.

With Buffalo (5-9) trailing 13-7, Allen stayed patient in the pocket and waited for Foster to complete an up-and-out to the right side of the field. Foster was wide open as he reached the right hash mark and caught Allen's pass in stride, and then beat a defender to the pylon with 10:56 remaining.

Quarterback Matthew Stafford responded by marching the Lions 45 yards on the next possession only to have kicker Matt Prater miss a 48-yard field goal wide right.

Special teams also cost the Lions a point after Stafford hit Andy Jones for a 4-yard touchdown to open the scoring. A bad snap by veteran Don Muhlbach led to Prater throwing the ball away on a failed extra-point attempt.

The game wasn't decided until Allen jumped over the pile for a 2-yard gain to convert a fourth-and-1 at Detroit's 41 with 1:47 remaining. The Bills snapped a two-game skid.

The Lions (5-9) lost despite rookie Kenny Golladay finishing with a career-best 146 yards receiving. Stafford finished 22 of 29 for 208 yards passing.

Detroit will finish with a losing record in its first season under rookie coach Matt Patricia, ending under .500 for the first time since going 7-9 in 2015.

Allen won for the third time this season when the Bills are behind or tied in the fourth quarter.

Allen led the Bills to victory by using his arm rather than his legs. He finished with just 16 yards rushing after becoming just the second quarterback in NFL history to top 95 yards rushing in three consecutive games.

And the Bills won despite an injury-depleted lineup.

Buffalo opened the game minus starting running back LeSean McCoy (left hamstring) and backup Chris Ivory (left shoulder), after both were hurt in a 27-23 loss to the New York Jets last weekend. The Bills then lost running back Marcus Murphy, who hurt his left arm late in the second quarter.

STAFFORD TO GOLLADAY

Stafford bounced back after managing just 101 yards passing — the second-fewest in his career _ in Detroit's 17-3 win at Arizona last week. He topped that number in the first half alone against the Bills with 137 yards passing, with Golladay leading the way with four catches for 115 yards.

Stafford went at the strength at the Bills secondary in beating Buffalo's top cornerback Tre'Davious White.

Golladay was falling backward to make a 33-yard catch even though White was flagged for pass interference to set up Detroit's first scoring drive, capped by Stafford's 4-yard pass to Andy Jones.

Trailing 7-6, Stafford floated a pass up the right sideline which Golladay caught by outjumping White for a 40-yard completion. The Lions scored three plays later on Zach Zenner's 1-yard plunge.

NOT-SO-SPECIAL TEAMS

Bills: A hip injury that bothered kicker Stephen Hauschka came into play on Buffalo's first possession, and after Allen was sacked for a 9-yard loss on third-and-9 at Detroit's 29. Rather than have Hauschka attempt a 50-plus-yard field goal, the Bills punted. Hauschka was hurt when being blindsided by Jets defensive lineman Henry Anderson last week.

UP NEXT

Lions: Close home schedule against Minnesota next Sunday.

Bills: Close road schedule at New England next Sunday.