The winning drive took nine plays and 60 yards with 2:52 taken off the clock.
Then, on fourth-and-five at the Miami 14, Thomas, in his first game after the Dolphins signed him as a free agent, got an interception in the end zone.
Brady passed for 364 yards, two touchdowns and the one interception.
Tannehill, who converted a fourth-and-five play on his final drive with a pass to tight end Charles Clay, threw for 312 yards, three touchdowns and no interceptions.
The Dolphins (8-6) won their third straight game and kept the Patriots (10-4) from clinching the AFC East division for the fifth straight year. The Patriots can still clinch with one win or tie or one Dolphins tie or loss in the next two weeks.
New England had its three-game win streak snapped. New England's streak of seven consecutive wins over the Dolphins -- dating to Dec. 6, 2009 -- also ended.
The Patriots scored the first time they got the ball, using 16 plays and 7:13 on the clock to travel 83 yards, settling for a 22-yard field goal by Stephen Gostkowski. One play before the field goal, the Patriots nearly had a touchdown, but receiver Josh Boyce had the ball bounce off his hands in the end zone after he was hit by cornerback Jimmy Wilson.
Miami had a chance to tie it or go ahead late in the second quarter. But the Dolphins decided not to go for it on fourth-and-1 and instead tried a 42-yard field goal. The decision backfired when John Denney's snap hit Brandon Fields in the face mask after the holder had looked back at kicker Caleb Sturgis.
The Patriots took advantage, driving 69 yards on seven plays, culminating in a 13-yard touchdown pass from Brady to tight end Michael Hoomanawanui with 1:38 left in the first half.
On the play, Brady moved around in the pocket and threw the ball when Hoomanawanui had his back to him deep in the end zone. The tight end looked over his shoulder just in time to beat linebacker Philip Wheeler.
The Dolphins went into halftime trailing 10-7 after an eight-play, 82-yard drive that took just 1:06 off the clock and ended with quarterback Tannehill's 39-yard touchdown pass to wide receiver Mike Wallace. He took a short slant pass and broke a tackle try by Patriots cornerback Marquice Cole.
Miami's drive was set up by a 42-yard diving catch by wide receiver Rashard Matthews on a third-and-10 play from its own 18.
The Dolphins tied the score 10-10 on Sturgis' 32-yard field goal on Miami's first drive of the third quarter. That came after Gostkowski missed wide left from 48 yards
Miami went up 17-10 on the first play of the fourth quarter, a 2-yard Tannehill pass to Daniel Thomas.
The drive, which took 10 plays and traveled 66 yards, was kept alive by a third down pass-interference penalty on Patriots defensive end Rob Ninkovich. Miami likely would have settled for a short field goal had the call not been made.
New England cut Miami's lead to 17-13 on a 23-yard field goal by Gostkowski. That capped a 15-play, 81-yard drive that took 7:09 off the clock.
The Patriots went up 20-17 on Brady's 24-yard touchdown pass to wide receiver Julian Edelman with 4:07 left in the fourth quarter.
NOTES: Attendance was 71,863. .. Patriots LT Nate Solder suffered a head injury in the second half and did not return. ... Dolphins DT Paul Soliai (ankle) and CB Nolan Carroll (knee) aggravated existing injuries Sunday. ... Among the Patriots' top four WRs, two of them were out with injuries Sunday -- Aaron Dobson (foot) and Kenbrell Thompkins (hips). A third WR, Danny Amendola, played despite a groin injury. ... The Patriots are also beat up at TE, where Rob Gronkowski (knee) is out, and Michael Hoomanawanui (knee) played hurt. ... The Patriots close the regular season with games Sunday at Baltimore and Dec. 29 at home vs. Buffalo. ... The Dolphins end their regular season with games Sunday at Buffalo and Dec. 29 at home vs. the Jets.