FOXBORO (CBS) — The New England Patriots are officially AFC East champs. It did not come easy.

The team faced a second-half deficit against the Bills on Saturday, needing a boost from their up-and-down offense to save the day. Tom Brady and Co. delivered, marching 77 yards for a field-goal drive and then 59 yards on a touchdown drive that included a two-point conversion from Brady to Julian Edelman to give New England a 24-17 lead.

READ MORE: Lawmaker Proposes Universal Mask Mandate In All Massachusetts Schools

That lead held thanks to a red zone stand by the New England defense, preserving the victory.

Brady went 26-for-33 for 271 yards with a touchdown and no interceptions. Josh Allen went 13-for-26 for 208 yards with two touchdowns and no picks.

Sony Michel rushed for 96 yards on 21 carries, while Rex Burkhead had a rushing touchdown.

Nick Folk was 3-for-3, including a 51-yarder, on field goals and 2-for-2 on PATs.

The win improves the Patriots to 12-3 and drops the Bills to 10-5. The Patriots clinched the division with the victory, but — barring a Kansas City loss this week or next — they’ll need to win next weekend against the Dolphins in order to secure a first-round playoff bye.

The game began on a promising note for New England, with Brady hitting Edelman for a gain of seven yards to convert a third-and-3 on the opening series. Brady then hit Rex Burkhead on a screen pass, which went for 18 yards, but Burkhead was stripped of the ball by Jordan Poyer. Micah Hyde returned it 31 yards, and the Bills kicked a field goal to take an early 3-0 lead.

The Patriots responded with a long and lengthy drive, featuring a pair of third-down conversions through the air, some hard running from Sony Michel, and a perfectly lofted touchdown pass from Brady to Matt LaCosse to give New England a 7-3 lead. Brady tied Peyton Manning on the all-time touchdown list, and LaCosse became the 76th different receiver to catch a touchdown from Brady.

The Patriots added to their lead with a field goal at the end of a 17-play, 81-yard drive that chewed up 8:58 off the clock. That drive featured Brady blocking downfield on a Harry reverse:

READ MORE: Peabody Police Help Track Down 5-Year-Old's Missing Wheelchair

It also featured Sony Michel converting on a fourth-and-1. A 12-yard connection from Brady to Edelman on third-and-14 set up a 36-yard field goal for Nick Folk.

The Patriots were poised to take the 10-3 lead into halftime, but Bill Belichick elected to go for it on a fourth-and-1 from the Buffalo 41-yard line with 36 seconds left in the half. The jet sweep to Harry was picked up by Kevin Johnson, who tackled Harry at the line of scrimmage to force a turnover on downs.

On the ensuing Buffalo possession, Allen broke off a 16-yard gain on a designed QB run, and he then connected with Dawson Knox for a 33-yard gain that was initially ruled a touchdown but, upon further review, was ruled to have been down at the 1-yard line. After the review, Allen floated a 1-yard touchdown to offensive lineman Dion Dawkins, who lined up as an eligible receiver and went uncovered into the end zone. Dawkins caught Allen’s pass, and the game headed to halftime tied at 10-10.

The Patriots regained the lead after forcing a Buffalo punt to start the half, followed by a seven-play, 44-yard drive that ended with a successful 51-yard field goal from Folk.

The Bills answered, though, when Allen got off a pass just before taking a sack, lobbing a deep ball over Devin McCourty’s head and into the waiting arms of John Brown, who made the catch and scampered forward for a 53-yard touchdown.

The Patriots rallied, though, first driving 77 yards before getting stopped in the red zone and settling for a Folk field goal to cut the Buffalo lead to one point.

The Patriots’ defense then forced a three-and-out, and Edelman returned to the game after being evaluated for a head injury. Edelman had a 30-yard catch-and-run to kick off the drive, and Brady later ran a QB sneak to convert a third-and-1. Burkhead scored on a first-and-goal from the 1-yard line, and Brady connected with Edelman on the two-point conversion to make it a 24-17 lead for New England.

The Bills drove deep into New England territory, but the pass rush forced a prayer from Allen on fourth-and-goal from the 15. J.C. Jackson broke up the pass, forcing a turnover on downs and essentially sealing the New England victory.

MORE NEWS: 'Masks And Vax Required': Some Mass. Businesses Taking COVID Precautions Into Own Hands

The Patriots will conclude their regular season next Sunday at home, against the Dolphins. The Bills will close out their regular season with a home date against the Jets.