MIAMI GARDENS, Fla. (AP) Jordan Phillips' eagerness to get the last word backfired on the Buffalo Bills.

The animated defensive tackle drew a costly penalty for taunting his former team, setting up a touchdown with nine minutes left that helped the Miami Dolphins rally past Buffalo 21-17 on Sunday.

Miami turned away two Bills drives down the stretch, and Charles Clay made a diving try but failed to catch Josh Allen's desperation fourth-down pass at the goal line in the final minute.

''This one hurts a lot,'' Allen said.

The rookie quarterback threw for 231 yards and ran for 135 to set a franchise record for rushing yards by a quarterback for the second week in a row. Miami won despite being outgained 415-175.

''It beats the heck out of the alternative,'' said Miami's Ryan Tannehill, who threw three touchdown passes .

The Dolphins (6-6) snapped a two-game losing streak and won for only the third time since Week 3, helping their slim playoff chances. The Bills (4-8) will play their customary role of spoilers in the final weeks, assured of finishing at .500 or worse for the 16th time in the past 19 years.

Phillips had said he was eager to settle old scores with the Dolphins after they released him in Week 5, and Miami was trying to rally from a three-point deficit when he tackled Kenyan Drake for a 4-yard loss deep in Bills territory.

Phillips rose, pushed Drake's leg away and drew a 15-yard penalty for taunting.

When asked to describe the call, Phillips said, ''Borderline is very generous. ... I hit someone's foot, and that's an unsportsmanlike conduct? OK.''

Bills coach Sean McDermott tempered his critique of Phillips' penalty.

''He's playing emotional football,'' McDermott said. ''He made a great play in the backfield, but at the same time, you can't beat yourself. If that's how they're going to call it, that's how they're going to call it, and we have to play smarter than that.''

On the play after the infraction, the Bills stopped Miami for another loss, and Phillips directed showy applause toward the Miami bench. Bills coaches then took him out of the game, and he watched from the sideline as Tannehill hit Kenny Stills with a 13-yard scoring pass for a 21-17 lead.

Allen then drove the Bills to the Miami 23 before he was sacked on consecutive plays, and Stephen Hauschka missed a 55-yard field goal attempt.

The Bills drove 65 yards in the final 2 1/2 minutes but again came up short - literally. A completion to Zay Jones at the 6 was overturned by a replay review. Allen's final pass was off-balance, wobbly and underthrown, but Clay said he should have made the grab.

''I just didn't come down with it,'' Clay said. ''You've just got to catch it. I feel like I let everybody down.''

Allen threw touchdown passes of 15 and 25 yards to Jones, and the duo also connected on a 2-point conversion. But Allen was intercepted twice by Xavien Howard and twice missed open receivers for potential scores.

A muffed punt by the Bills' Isaiah McKenzie led to the Dolphins' second touchdown, which put them ahead 14-6.

FIERY BILLS

After the game, Phillips walked toward the Miami sideline and traded hugs with many of his former teammates.

Meanwhile, Bills defensive end Jerry Hughes confronted an official in the tunnel near the locker room. Hughes accused the official of calling him a vulgar name.

''I'll catch you. I'll catch you, guaranteed,'' Hughes shouted, waving his index finger, as he was pulled away by Bills officials.

Hughes later declined to elaborate on the reason for his anger.

''I was angry?'' he said. ''I lost the game, so I am supposed to be angry.''

INJURY REPORT

Bills C Russell Bodine left in the first quarter with a left leg injury and did not return.

Dolphins reserve CB Cornell Armstrong left with a knee injury.

UP NEXT

The Dolphins play host to AFC East New England on Sunday. The Patriots won the earlier meeting 38-7 in Week 4.

The Bills play at home Sunday against the New York Jets, who lost an earlier meeting 41-10 in Week 10.

---

Follow Steven Wine on Twitter: http://twitter.com/Steve-Wine

---

More AP NFL: https://apnews.com/NFL and https://twitter.com/AP-NFL

Copyright 2018 by AP. Any commercial use or distribution without the express written consent of AP is strictly prohibited.