Safety Jairus Byrd forced a fumble and had an interception, and Bryan Scott sealed the victory with an interception that ended the Dolphins’ last-gasp drive in the final minute. Williams had one of three sacks for a Buffalo defense that limited the Dolphins to 184 yards and 16 first downs.

That more than made up for an offense that lacked finish. Rian Lindell hit four field goals, including a 42-yarder, as Buffalo failed to score an offensive touchdown despite four trips inside the Miami 20.

Leodis McKelvin opened the scoring 90 seconds in by returning a punt 79 yards for a touchdown in a game the Bills never trailed.

The Bills (4-6) snapped a three-game skid and won for only the second time in seven games to stay on the fringe of the AFC playoff hunt. Buffalo also ended several other inglorious skids, by beating a division rival for the first time in nine meetings, and snapping an 11-game prime-time drought that dated to 2001.

“It was definitely a must win,” Williams said. “With the way things turned out earlier in the season, we’ve got to come out and play like it’s the playoffs. And we’ve got to make plays and we’ve got to finish. It got close there at the end, but I’m glad we came out with it.”

Williams was referring to how the Dolphins cut the Bills lead to 19-14 with 8:42 left when Ryan Tannehill hit Davone Bess with a 2-yard touchdown pass. That was as close as they got, as their final two drives ended with the rookie throwing interceptions.

Marcus Thigpen scored on a 96-yard kickoff return for Miami (4-6), which has lost three straight.

Tannehill’s struggles continued. He finished going 14 of 28 for 141 yards with a touchdown and two interceptions. He was coming off a dreadful outing in which he threw three interceptions in a 37-3 loss to Tennessee last week. The No. 8 pick out of Texas A&M has thrown six TD passes and 11 interceptions.

“It’s really frustrating, especially with the success earlier in the year,” Tannehill said. “Being in situations where we’re going three-and-out or six-and-out, it’s frustrating. We have to be able to find a rhythm and get things going early on.”

It wasn’t all Tannehill’s fault as the usually sturdy Dolphins defense gave up 120 yards rushing, including 91 to C.J. Spiller, who took over the bulk of the rushing duties in place of Fred Jackson (concussion).

After allowing 307 yards rushing in its first five games, Miami has given up 661 in its past five.

The Dolphins’ running game also sputtered.

Reggie Bush finished with 20 yards rushing, which marked a significant drop-off after he had a career-best 203 yards in 30-23 win last December during his previous visit to Orchard Park.

Miami finished converting just three of 10 third-down chances.

For Buffalo, it was a long-awaited breakout performance for a high-priced defense that entered the game among the NFL’s worst. Buffalo had ranked 31st in yards allowed and last in allowing an average of 31.67 points.

Williams was supposed to be the defense’s centerpiece after signing a six-year, $100 million contract March. He’s finally starting to play up to the deal, and his sack gave him a team-leading 5 1/2.

The unit forced three turnovers after failing to produce one its past three games.

The 184 yards allowed were a season-low for Buffalo, and the fewest the defense has allowed since limiting Washington to 178 in a 23-0 win Oct. 30, 2011.

Buffa lo didn’t allow the Dolphins to cross midfield until their eighth drive, 11 minutes into the third quarter. And that drive ended with Dan Carpenter missing a 50-yard field goal attempt in which the ball glanced off the outside of the right goal post.

Bills quarterback Ryan Fitzpatrick finished 17-of-27 for 168 yards passing. Spiller also had three catches for 39 yards, while Stevie Johnson had six catches for 79 yards.

Fitzpatrick gave much of the credit to how well the team’s defense played.

“The defense has been working so hard,” Fitzpatrick said. “There’s been a lot of criticism on them, but tonight was a dominating performance and something we can build on.”

The Bills bounced back from a 37-31 loss at New England. And the defense was questioned after allowing a combined 1,201 yards over a two-game stretch in which the Bills were blown out 52-28 to New England and 45-3 at San Francisco.

Miami managed just 50 yards and two first downs in the first half. Thigpen provided the Dolphins their only points.

His kickoff return cut the Bills lead to 10-7 with 4:55 left in what proved to be a wild first quarter that began with McKelvin’s touchdown.

The last time there was a punt return touchdown by one team and a kickoff return touchdown by the opposing team in the same quarter was December 1999 in a game between the Washington Redskins and Detroit Lions according to STATS LLC. Washington’s James Thrash had a 95-yard kickoff return for a score and Detroit’s Desmond Howard had a 68-yard punt return for a touchdown in the second quarter.