By Michael Hurley, CBS Boston

FOXBORO (CBS) —  All the Patriots needed to secure a first-round playoff bye was a victory over the Miami Dolphins, a team that entered Sunday’s game with a 4-11 record and nothing in the world to play for. It was expected to be a cinch for the Patriots … right up until kickoff.

READ MORE: CDC Panel To Vote On Whether To Recommend Pfizer Vaccine For Kids 12-To-15

The Dolphins stunned the Patriots — and the football world — by beating New England 27-24 in Foxboro.

As a result, the Patriots finished the season with a 12-4 record, and they’ll now be playing on Wild Card Weekend for the first time since 2009.

This one came down to the wire. Trailing 20-17, the Patriots got good field possession thanks to a 38-yard kick return by Brandon Bolden. Tom Brady connected with Mohamed Sanu on a deep crossing route for a 22-yard gain on a third-and-4, and Brady hit James White on a screen pass for a 13-yard touchdown on a third-and-goal following a false start penalty. Nick Folk hit the extra point, giving the Patriots a 24-20 lead with 3:53 left in the game.

The Dolphins then took over with a chance to score a go-ahead touchdown, with plenty of time and two timeouts in their pocket. On a second-and-12 near midfield, Ryan Fitzpatrick fumbled a shotgun snap but barely recovered the loose ball in time to keep the ball away from Adam Butler. On the next play, DeVante Parker went up and over Stephon Gilmore to haul in a 24-yard completion to get the ball to the New England 29-yard line.

Three plays later, Miami converted a third-and-5 with a 7-yard connection from Fitzpatrick to Albert Wilson.

With a first-and-10 from the 17-yard line, Fitzpatrick connected with Parker for a gain of eight yards. With 35 seconds left, Fitzpatrick hit Isaiah Ford on a slant to set up a first-and-goal from the Patriots’ 5-yard line with just 29 seconds left.

On first-and-goal, Fitzpatrick delivered a strike to Mike Gesicki, running a slant in the end zone, giving Miami a go-ahead touchdown with just 24 seconds left to play.

The Patriots took over at their own 25-yard line with just 24 seconds to play, but their desperation drive failed, despite a frantic series of laterals on the final play of the game.

Brady completed just 16 of 30 passes for 221 yards with two touchdowns and a very costly interception, which was returned for a touchdown. He also moved into the No. 2 spot on the all-time list for regular-season passing touchdowns.

Ryan Fitzpatrick completed 28 of his 41 passes for 320 yards with one touchdown and no interceptions.

The game was tied 10-10 at halftime, and the Patriots promptly went three-and-out to start the second half. The Dolphins then mounted a methodical 83-yard touchdown drive, highlighted by a trick play that featured a 20-yard pass by receiver Albert Wilson to running back Patrick Laird. Fitzpatrick scrambled into the end zone from the 2-yard line after connecting with tight end Mike Gesicki for a 14-yard gain, giving the Dolphins a 17-10 lead midway through the third quarter.

But the Patriots responded in stunning fashion, when Brady targeted linebacker-turned-fullback Elandon Roberts for the first time all season. Roberts was completely uncovered, and despite an off-the-mark pass by Brady, Roberts made a tremendous catch while spinning around in the open field. After securing the pass, Roberts was off to the races, shaking one tackle en route to scoring a 38-yard touchdown to tie the game at 17-all.

With that pass, Brady moved past Peyton Manning for second place on the all-time touchdown pass list, and Roberts became the 77th different receiver to catch a touchdown pass from Brady.

The game got out to a stunningly slow start for the home team. After Miami threw incomplete on a fake punt to start the game, the Patriots immediately went three-and-out and punted to Miami. The Dolphins then embarked on a long drive that took them 80 yards on 12 plays. Fitzpatrick, though, missed a relatively easy touchdown pass to Isaiah Ford, forcing Miami to settle for a chip shot field goal to take a 3-0 lead.

The Patriots were in the midst of responding at the end of the first quarter, but Brian Flores challenged a play, arguing that Ben Watson committed pass interference on a key third-down completion to Mohamed Sanu. Flores won the challenge, forcing the Patriots into a third-and-20 and, eventually, another punt.

The Dolphins stretched their lead to 10-0 when Brady made an uncharacteristically bad throw. Former Patriots cornerback Eric Rowe jumped in front of the pass, picked it off, and returned it 35 yards for a pick-six. It was Brady’s first-career pick-six against Miami.

The Patriots responded with a lengthy drive of their own, powered by big runs by Sony Michel and Rex Burkhead, a third-down pass interference penalty on Nate Brooks, and an 18-yard catch by N’Keal Harry up the left sideline. But the drive stalled at the Dolphins’ 7-yard line, leading to a short Nick Folk kick to cut the Miami lead to 10-3 with 5:51 left in the first half.

The Patriots then tied the game with an 82-yard touchdown drive, aided by a personal foul call against Miami and a 50-yard connection between Brady and Phillip Dorsett.

Michel plowed into the end zone for a 4-yard touchdown run. That tied the game just before the two-minute warning.

The Patriots came up with a stop on defense but elected to not use any timeouts prior to Miami’s punt, and the Patriots then just ran out the clock instead of trying to score, taking three timeouts with them into halftime.

MORE NEWS: Employer Vaccination Program Coming Soon In Massachusetts

The Patriots will finish the year with the No. 3 seed in the AFC, and they will be hosting a wild card game next weekend in Foxboro, thus making the road to Super Bowl LIV much longer and steeper.