ORCHARD PARK, N.Y. (AP) — Bills rookie Josh Allen had a career-best three touchdown passes and scored two more rushing, and defensive tackle Kyle Williams closed his 13-year career in Buffalo with a win, 42-17 over the Miami Dolphins on Sunday.

Williams had a tackle for a loss and was part of a defense that forced four turnovers, including three by Miami quarterback Ryan Tannehill, in a game between two AFC East teams already eliminated from playoff contention.

The 35-year-old Williams has been the team's unquestioned leader and was honored before the game by being the last player out of the tunnel and then was greeted on the field by his wife, Jill, and their five children. Williams announced on Friday that he was retiring.

The Bills then called timeout with 1:19 left, when Williams walked off the field for the final time and hugged coach Sean McDermott on the sideline.

Leave it to Allen, who represents the Bills' future, to put the finishing touches on a 6-10 season in which Buffalo won four of its final seven games. The 42 points scored were the most by Buffalo since a 45-16 win over San Francisco on Oct. 16, 2016.

Though the Bills never trailed, Allen's 5-yard touchdown pass to Robert Foster six minutes into the third quarter broke a 14-14 tie to put Buffalo ahead for good. Allen then secured the victory with a 30-yard TD run to increase Buffalo's lead to 35-17 with 11:13 left in the game.

He finished 17 of 26 for 224 yards passing, including 18- and 26-yard touchdowns to Zay Jones, and an interception. And he added 95 yards rushing, including a 1-yard touchdown to open the scoring.

The Dolphins (7-9) thoroughly unraveled except for the final minute of the second quarter, in which they scored twice in a span of 16 seconds to tie the game at 14.

The Dolphins now enter an offseason of uncertainty, with questions raised about the futures of coach Adam Gase and vice president of football operations Mike Tannenbaum as well as Tannehill, who has two years left on his four-year contract.

Tannehill finished 18 of 31 for 147 yards with two interceptions and a lost fumble. He did score by catching a 3-yard pass from Kenny Stills with 54 seconds left in the second quarter. Dolphins safety Reshad Jones scored two plays from scrimmage later on a 29-yard interception return.

Otherwise, the Dolphins concluded a season in which they won their first three games with three losses, and lost their seventh consecutive road game.

Miami's defense allowed 381 yards to finish the season with a franchise-worst 6,257 yards allowed. Its previous worst was 6,122, set in 2016.

Williams got to enjoy his final game by contributing on both offense and defense. On first-and-10 from Miami's 40, Williams caught a 9-yard pass with under five minutes left. The crowd cheered and began chanting ``Kyle Williams!'' after he made the catch and was pushed out bounds. With 2 minutes left, the crowd stood and began chanting: "Thank you, Kyle!''

The only thing missing was a sack from Williams, who closed the season with five and his career with 48 — the most by a Bills defensive tackle.

Tannehill was sacked four times, including twice by Shaq Lawson.

EJECTIONS

Dolphins linebacker Kiko Alonso and defensive end Robert Quinn and Bills offensive tackle Jordan Mills were ejected following a late hit on Allen during the third quarter.

Allen was already sliding when Alonso dived in front of him and kicked the quarterback in the helmet. A scuffle broke out amid a group of players, and Quinn came running in at full speed from about 20 yards out and dived atop the pile. Alonso was ejected for kicking Allen, while Quinn and Mills were ejected for personal foul penalties.

RUNNING QBS

Allen's eight rushing touchdowns this season were the second-most by a quarterback in the Super Bowl era. Cam Newton had 14 in 2011. Robert Griffin III, in 2012, and Vince Young, in 2006, each scored seven.

With a 35-yard scamper up the left sideline in the third quarter, Allen topped the single-season team record for yards rushing by a quarterback. Tyrod Taylor had 580 yards two years ago. Allen finished with 630 in just 11 games.

'ZO's FUTURE?

Though Williams is retiring, another Bills veteran leader, linebacker Lorenzo Alexander, said on Friday he is interested in re-signing with the team after his contract expires. General manager Brandon Beane is more than open to bringing back the 35-year-old Alexander for a fourth season.

"If his heart is set on playing another year, we'd love to get him back, and we'll work to do that,'' Beane said.

INJURIES

Dolphins: CB Torry McTyer did not return and was being evaluated for a concussion after being bowled over by Buffalo's Logan Thomas. McTyer was looking up the field and didn't see Thomas coming in from his right. McTyer started in place of Xavien Howard, who missed his fourth straight game with a knee injury.

Bills: CB Tre'Davious White did not return after sustaining a head injury in the final minutes of the first half. White was knocked over by Kenyan Drake while lowering his head as he tried to make an open-field tackle.