MIAMI GARDENS, Fla. -- Thad Lewis completed one pass for a first down even as a blitzing linebacker knocked off his helmet and sent it skittering upfield.
The hit drew a flag and left Lewis sprawled on the turf, but the Buffalo Bills' young quarterback leaped to his feet screaming and punching the air to celebrate the gain.
"I was excited," he said. "I'm not a rah-rah guy, but when you're down and out, you've got to do something to get the team going."
Lewis was sacked four times and tossed an interception, but he threw for 202 yards and helped the Bills convert nine of 19 third downs.
The defense did the rest. Rookie Nickell Robey returned an interception 19 yards for a touchdown on the third play of the game, and Buffalo turned the tide late with the help of two sacks by Williams.
"It meant a lot to get this win," he said. "Wins are hard to come by in this league. It's even sweeter to get it at home."
Miami (3-3) lost its third game in a row. A month ago, the Dolphins were basking in their best start since 2002. They haven't won since.
"We're in a tough spot right now," Tannehill said. "We have to face the adversity and grow from it. There are two ways we can go. We can tank it, or we can turn it around."
The 99th meeting between the AFC East rivals had one of the wilder endings in the series. With Miami nursing a 21-20 lead, Tannehill tried to throw from midfield but fumbled when sacked by Williams.
"We needed to get the ball back in good field position," Williams said. "When I came through, I realized I had a shot at it. I thought, 'There's the ball.' Then it came out."
Kyle Williams recovered at the Miami 34. Carpenter, who drew boos throughout the game, lined up for the winner seven plays later.
"I just thought, 'Kick it through those yellow things,'" he said.
Marcus Thigpen nearly broke the ensuing kickoff for a touchdown, but he was tripped up by Carpenter after a 44-yard return to the Miami 46. Tannehill then threw four consecutive incompletions, his final desperate heave landing in the end zone among a cluster of players as time ran out.
Along with his fumble and interception returned for a touchdown, Tannehill threw another interception when Miami was threatening. Tannehill, who came into the game on pace to set an NFL record for being sacked, increased his total to 26 with two -- both by Mario Williams, who has 10 this year.
"I have to take full responsibility," Clabo said. "He's a really good player. He made a big play at a crucial point in the game. That's why he's considered one of the best defensive ends in the league."
Gibson turned one short throw into an acrobatic score, leaping over one defender at the goal line and splitting two others as he tumbled airborne into the end zone.
His spinning limbs made him look like a helicopter, and the 13-yard score with 21 seconds left in the first half cut Buffalo's lead to 17-14.
Gibson made his second touchdown catch just as entertaining. After catching a 4-yard pass in the back of the end zone, Gibson flipped the ball aside, leaped over a barricade and was mobbed by more than a dozen happy fans watching from field level.
Notes: Bills linebacker Manny Lawson left the game in the third quarter with a hamstring injury. ... Dolphins defensive end Cameron Wake saw sporadic action after missing most of the past three games with a left knee injury. ... Miami conerback Dimitri Patterson had a sack in his first game since suffering a groin injury in the season opener. ... The Bills have scored at least 20 points in every game.
Copyright 2013 by The Associated Press