Gregory Fisher-USA TODAY Sports

Josh Allen threw three touchdown passes and the Buffalo defense stood firm in the fourth quarter as the Bills defeated the visiting Miami Dolphins 34-31 in an AFC wild-card game Sunday in Orchard Park, N.Y.

The second-seeded Bills forced the Dolphins into a fourth-and-1 near midfield late in the game. Miami took too long getting to the line of scrimmage to snap the ball, causing a delay-of-game penalty, and Skylar Thompson threw incomplete on fourth-and-6.

The seventh-seeded Dolphins were out of timeouts. Buffalo running back Devin Singletary reached the line to gain on third-and-7 to ensure the result.

Buffalo eliminated Miami and will host a divisional-round game next weekend as its players and fans continue to be inspired by Damar Hamlin. The Bills' safety watched Sunday's game from home as he continues to recover from a cardiac arrest he suffered on the field against Cincinnati on Jan. 2.

Allen went 23-for-39 passing for 352 yards and had three total giveaways vs. the Dolphins. Stefon Diggs caught seven passes for 114 yards, and Gabe Davis had six for 113 and a touchdown.

Thompson, a rookie quarterback starting for Miami in place of the injured Tua Tagovailoa and Teddy Bridgewater, finished 18-for-45 for 220 yards, a touchdown and two interceptions.

The Bills led 17-3 during the second quarter, but two interceptions and a lost fumble by Allen allowed the Dolphins to surge ahead shortly after halftime.

In the first half, Xavien Howard made a fingertip interception on a deep pass and returned it 49 yards into Buffalo territory. Miami converted a fourth down to get into field-goal range, and Jason Sanders connected from 48 yards with 2:15 left.

The Dolphins then forced a three-and-out, and Cedrick Wilson Jr. returned the punt 50 yards, setting up Sanders' third field goal of the day, from 37 yards, with 1:15 on the clock.

Allen targeted Cole Beasley on the ensuing drive, and the ball bounced off his hands to Jevon Holland, who returned it 29 yards to the Buffalo 18. Thompson found Mike Gesicki for a 7-yard touchdown and completed another pass to Hill for the game-tying two-point conversion.

With 33 seconds to spare, the Bills drove downfield, and Stefon Diggs appeared to haul in a touchdown down the left side of the end zone, but he didn't have complete control before stepping out of bounds and officials would not review it. The hectic half ended with Bass' 39-yard field goal for a 20-17 Buffalo lead.

On Buffalo's first play on offense of the second half, Eric Rowe blitzed Allen and jarred the ball loose. Zach Sieler returned it 5 yards for a touchdown, giving Miami its first lead at 24-20.

After Kaiir Elam intercepted a Thompson pass, Buffalo capitalized with Beasley's 6-yard touchdown catch. The extra point made it 27-24 Bills with 5:18 left in the third.

Gabe Davis added a 23-yard catch in the back-left corner of the end zone to restore the Bills' double-digit lead, 34-24, with 2:07 left in the third. The Dolphins traveled 75 yards on their next drive, and Wilson scored from 1 yard to cut the deficit to three with 10:53 to play, but they could not get closer.

More must-reads:

TODAY'S BEST
Pacers' Pascal Siakam leads team to Game 6 win vs. Knicks
Late goal sends Panthers to Eastern Conference Finals
Scottie Scheffler shoots improbable 66 after warming up for PGA Championship in a jail cell
Report: Tua Tagovailoa away from Dolphins amid contract chatter
Nuggets star has worrying comment about latest injury
Paul Skenes makes incredible Wrigley Field history in second-career MLB start
Giants rookie CF to undergo season-ending labrum surgery
Yankees' Juan Soto reacts to Hal Steinbrenner contract talk
Ex-teammate of Shohei Ohtani placed bets with same illegal bookmaker as interpreter 
Former Rams first-round pick retires from NFL after 11 seasons
Insider provides major injury update on Celtics' Kristaps Porzingis
Watch: Bruins strike first in Game 6 with incredible backhand goal
Dodgers make series of moves involving notable players
Hurricanes not expected to re-sign defenseman, center
Maple Leafs tab former Stanley Cup winner as new head coach
NFL insider expands on competition between Steelers QBs Russell Wilson, Justin Fields
NFL sets outrageous prices for Eagles-Packers Brazil game
Broncos 'very unlikely' to bring back former NFL interceptions leader
Greg Olsen offers broadcasting advice to Tom Brady
Mets star has theory about closer Edwin Diaz's recent struggles