MIAMI GARDENS, Fla. -- MIAMI GARDENS, Fla. (AP) -- When the Buffalo Bills needed the ball back, Mario Williams knocked it loose.
Williams forced a fumble by sacking Ryan Tannehill with less than three minutes left, setting up the winning field goal to help Buffalo beat the Miami Dolphins 23-21 Sunday.
Kicker Dan Carpenter, released in August after five seasons with the Dolphins, beat his former team by making a 31-yarder with 33 seconds to go.
Rookie Nickell Robey returned an interception 19 yards for a touchdown on the third play of the game to help the Bills build a 14-0 lead. But they had to rally after Brandon Gibson caught his second touchdown pass to put Miami ahead in the third quarter.
The injury-plagued Bills (3-3) ended a streak of six consecutive road losses, including two this year, while 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.
The Bills won with Thad Lewis at quarterback in his second consecutive start since being promoted from the practice squad to replace injured EJ Manuel. Lewis was sacked four times and threw an interception, but helped the Bills convert 9 of 19 third downs.
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. Kyle Williams recovered at the Miami 34, and Carpenter kicked the winner seven plays later.
Marcus Thigpen nearly broke the ensuing kickoff for a touchdown, but 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.
Tannehill almost offset his mistakes with three touchdown passes. 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 TD catch just as entertaining. After catching a 4-yard pass in the back of the end zone, he flipped the ball aside, leaped over a barricade and was mobbed by more than a dozen happy fans watching from field level.
The crowd was more subdued when Robey jumped a short route to intercept Tannehill and somersaulted across the goal line for a 7-0 Bills lead. Tannehill cost the Dolphins again when his ill-advised lob was intercepted at the goal line by Aaron Williams.
Miami rookie Don Jones was penalized when he came from out of bounds to down a punt at the 15, and the mistake cost Miami 40 yards when they were forced to re-kick. Buffalo took advantage of the break in field position to score six plays later for a 14-0 lead.
The Dolphins' Caleb Sturgis missed a 51-yard field goal when his kick hit the right upright, which ultimately made the difference in the game.
Lewis, who grew up near the Dolphins' stadium, exemplified the Bills' resilience. He completed one pass for a first down even as a blitzing linebacker knocked Lewis' helmet off his head, and the quarterback got up hollering and throwing punches at the air.
The Bills won for only the third time in their past 18 road games. They won for the first time in three games this season against division opponents.
AP NFL website: www.pro32.ap.org and http://twitter.com/AP_NFL
Follow Steven Wine on Twitter: http://twitter.com/Steve_Wine