MIAMI GARDENS, Fla. (AP) — The Miami Dolphins’ defense stopped Josh Allen and the Bills at the goal line late, then held again after punter Thomas Morstead bizarrely kicked the ball off a teammate and out of bounds for a safety in a 21-19 win over Buffalo on Sunday.

Tua Tagovailoa and the Dolphins improved to 3-0 in an AFC East thriller that ended with Allen and the Bills attempting to spike the ball near midfield, the clock just running out on their chance to try a long-distance, go-ahead field goal.

Tagovailoa briefly left the game in the second quarter, returning after halftime despite appearing to be disoriented by a head injury. Allen also may have been injured, heading to the X-ray room after the game. He had his throwing hand checked by trainers late.

Miami took the lead with about 10 minutes left in the game, scoring a touchdown to go up 21-17. It was the Bills’ first deficit of the season.

Despite being sacked and flagged for unnecessary roughness on the next drive, Allen drove Buffalo down the field with a 17-play, eight-minute drive. Miami’s defense held, though, nearly intercepting a pass and forcing a turnover on downs from the 2-yard line.

“You know we really beat ourselves tonight,” said Josh Allen. “Credit to them, they had a really great game plan and they came out and did what they had to do. But there are obviously plays that we want back and that’s going to happen over the course of every single game, this one especially.”

Tagovailoa and the Dolphins went three-and-out, leaving little room for Morstead to try punting out of the end zone. His kick hit blocker Trent Sherfield in the backside and caromed out of bounds, a play immediately dubbed the “butt punt” on social media.

Allen had about a minute to guide Buffalo into field goal range, and did so, but the Bills ran out of time. Isaiah McKenzie tried for extra yards on a completion from Allen, costing precious seconds. Dolphins players started celebrating when the clock hit zero with Allen just stepping under center.

Bills offensive coordinator Ken Dorsey slammed his headset and began throwing paper and notebooks in the coaches’ box when time ran out.

“I think we’ll learn things that were really good about today, one of those is the guts, and the heart, and the intangibles of our football team,” said Bills head coach Sean McDermott. “And we’ll learn some things that weren’t very good today, which lead to the result that we got. I mean when you look at the difference of the game, we had almost 500 yards to their 212, you should win those games.”

Allen was 42 of 63 and passed for 400 yards after completing his first 11 passes for the first time in his career. He had touchdown passes to running back Devin Singletary and receiver Isaiah McKenzie.

The Bills were without several defensive starters, including starting safety Micah Hyde, who on Saturday went on injured reserve with a neck injury, and cornerback Dane Jackson, who suffered a neck injury against the Titans last week. The Dolphins’ 21 points was the most Buffalo has allowed all season.

“You know we’ve got high character guys like this, adversity reveals character,” said Von Miller. “This is all just adversity. I’m excited to see how we respond. Real men, they respond to things like this and I’m excited to see how we respond.”

Tagovailoa slammed his head on the turf after a late hit from linebacker Matt Milano. He stumbled and fell to the ground trying to walk it off and was taken to the locker room immediately, but he returned to begin the third quarter.

Tagovailoa completed 13 of 18 passes for 186 yards, one touchdown and no turnovers.

The Bills scored on their opening drive for the eighth straight game. It was a two-yard touchdown throw from Allen to Singletary, set up by 28- and 19-yard catch and runs by Diggs and Singletary earlier in the drive.

In the fourth quarter, Miami’s Jaylen Waddle caught a 32-yard pass along the right sideline, then added a 45-yard reception on third-and-22 to set up Chase Edmonds’ eventual 2-yard touchdown run.

Edmonds also had a 1-yard touchdown run in the first quarter.

The Dolphins’ other touchdowns were a 1-yard run by Edmonds in the first quarter and a 2-yard catch by undrafted rookie receiver River Cracraft.

TOO HOT TO HANDLE

Several Bills players had trouble adjusting to the South Florida heat. McKenzie, TE Dawson Knox and T Spencer Brown were all listed with heat illnesses. Players went down on the field with cramps at multiple points in the game.

INJURIES
Bills: WR Jake Kumerow injured his ankle in the first quarter and did not return. … T Spencer Brown, listed with heat illness, did not return. … CB Christian Benford injured his hand.

Dolphins: T Greg Little injured his finger. CB Xavien Howard left in the third quarter with cramping.

UP NEXT:
Bills: At the Baltimore Ravens next Sunday.
Dolphins: At the Cincinnati Bengals on Thursday.