MIAMI -- Beating his former team was nothing special for kicker Dan Carpenter.
Beating a division rival on a day the AFC East-leading New England Patriots also lost was a different story.
Carpenter made a 31-yard field goal with 36 seconds left, and quarterback Thad Lewis, a Miami native, won his first NFL game as the Buffalo Bills defeated the Miami Dolphins 23-21 Sunday afternoon at Sun Life Stadium.
"I don't have any hard feelings toward the Dolphins," said Carpenter, who went 3 for 3 on field goals on the day. "But it's got extra meaning because it's a division game. We were just watching in (the locker room), the Jets beat the Patriots. (The division) is getting mixed up."
The Patriots lead the AFC East at 5-2, followed by the 4-3 New York Jets, 3-3 Dolphins and 3-4 Bills.
But that might have been a bit different had Carpenter not made a key stop on the ensuing kickoff return by Marcus Thigpen, breaking up a possible touchdown on Miami's final possession.
Thigpen, tripped up by Carpenter, ran out of bounds at Miami's 46.
"I didn't hit it very well," Carpenter said of the kickoff. "I put our team in a really tough spot. I'm glad our defense picked me up."
The Bills stopped the Dolphins on four straight passes to end the game, including the final heave into the end zone.
The winning field goal was set up by Mario Williams' strip sack of Ryan Tannehill. The fumble was recovered at the Miami 34 by Kyle Williams.
It was one of Tannehill's three turnovers on the day, including two interceptions.
"It was a play we felt we knew," Dolphins coach Joe Philbin said, "and a play we felt we could execute."
A 10-yard run by Fred Jackson on that final Buffalo drive was also key. He converted a third-and-4 play from the Miami 28, eating more clock and setting up a closer field goal.
Buffalo, which snapped a two-game losing streak, will play at the New Orleans Saints next Sunday.
The Dolphins, who have lost three games in a row, will play at New England next Sunday.
Lewis, playing because of the injury to E.J. Manuel, is now 1-2 as an NFL starter.
Bills coach Doug Marrone said the key with Lewis was settling him down.
"He was all fired up," Marrone said of Lewis, who completed 21 of 32 passes for 202 yards and one interception. "I told him: 'Just because you are in your hometown doesn't mean you can go off the deep end.' "
Miami took a 21-17 third-quarter lead on a 4-yard Tannehill pass to Brandon Gibson, their second TD connection of the day. Tannehill capped the 11-play, 66-yard drive by scrambling to his right before finding Gibson in the right corner of the end zone.
Buffalo closed to within 21-20 with 14:16 left in the fourth quarter on a 20-yard field goal by Carpenter.
"We did a good job containing them," Dolphins defensive end Dion Jordan said of a Buffalo offense that scored just one touchdown. "We just didn't finish."
The Bills opened the first-quarter scoring when backup defensive back Nickell Robey jumped a Gibson out route, intercepting Tannehill's first pass of the game and returning it 19 yards for a touchdown.
Jackson, who left the game on his team's first possession after suffering a knee injury, returned on the next drive to score a 3-yard touchdown. That capped a six-play, 44-yard march after the Bills pinned Miami deep in its own territory.
"I was scared," Marrone said of his reaction to Jackson's injury.
The Dolphins ended the first quarter trailing 14-0 after Tannehill was picked off again, this time in the red zone. He tried to lob the ball to Brian Hartline but was instead intercepted by safety Aaron Williams at the goal line.
With 10:59 left in the first half, the Dolphins got on the board with a 7-yard pass from Tannehill to tight end Charles Clay.
The Dolphins got the ball right back at the Buffalo 31 after safety Chris Clemons intercepted Lewis on a pass over the middle intended for Stevie Johnson.
But Miami could not move the ball, and Caleb Sturgis' 51-yard field goal attempt was wide right.
Buffalo extended its lead to 17-7 with a 39-yard field goal by Carpenter.
The Dolphins closed the first-half scoring with a 13-yard touchdown pass to Gibson with 21 seconds left. Gibson took a hit and vaulted about 5 yards to complete the play, capping a 75-yard drive that took just 1:04 and closed Miami's deficit to 17-14.
Notes: Johnson returned to the starting lineup after missing one game due to a back injury. ... Robey is a native Floridian (Frostproof). ... Bills LB Manny Lawson left the game in the third quarter due to a hamstring injury. ... Dolphins DE Cameron Wake, who missed all but three plays of the past two games due to a knee injury, returned to the starting lineup. ... The Dolphins started Jason Trusnick at MLB in place of leading tackler Dannell Ellerbe (shoulder injury). It was just the eighth start in the past four years for Trusnick, 29.