Josh Allen threw for 375 yards, tied a career-high with four touchdown passes and the Buffalo Bills stayed in sole possession of first place in the AFC East with a 34-24 victory over the San Francisco 49ers on Monday night.

Buffalo (9-3) moved a step closer to winning its division for the first time since 1995 thanks to a nearly flawless performance from Allen. The 24-year-old quarterback was stellar against the 49ers, completing 32 of 40 passes with no interceptions.

He threw touchdown passes to Cole Beasley, Dawson Knox, Isaiah McKenzie and Gabriel Davis as the Bills built a 17-7 halftime lead and controlled the majority of the second half.

CLICK HERE FOR MORE SPORTS COVERAGE ON FOXNEWS.COM

The Bills are one game ahead of the Miami Dolphins with four games left for both teams.

San Francisco (5-7) lost in its first game at its adopted home in Arizona. The 49ers will be based in Glendale for at least the next three weeks after Santa Clara County issued strict new coronavirus protocols that forced the team to find a temporary new home.

It was Allen's fourth game of the season with at least 300 yards passing and three touchdowns, which set a franchise record. Jim Kelly did it three times in 1991 and Drew Bledsoe three times in 2002.

The Bills had a much more pleasant trip to the desert than three weeks ago, when they lost a 32-30 heartbreaker to the Arizona Cardinals. That was the game when DeAndre Hopkins made a stunning catch over three Buffalo defenders with 2 seconds left for the game-winning score.

San Francisco has had several recent injuries at its slot cornerback position and struggled to cover Beasley, who had a career-high 130 yards receiving on nine catches.

State Farm Stadium — which is home to the division-rival Arizona Cardinals — was dressed up to make the 49ers feel slightly more at home. There were San Francisco banners hanging on the walls along the sidelines and the videoboard showed Niners highlights and flashed messages like “Faithful to the Bay."

The teams traded goal-line stands in the first quarter.

The Bills opened the game with a 74-yard drive that was stopped at the Niners 1 after Allen's fourth-down pass fell incomplete. San Francisco responded with a 97-yard drive that ended at the Buffalo 2 when Jeff Wilson Jr. was stuffed for no gain on fourth down.

It was just the second game since 2000 that featured two turnovers on downs to start a game.

But San Francisco got the ball back one play later on a fumble by Allen and the 49ers took advantage. Nick Mullens hit Brandon Aiyuk in the middle of the end zone for a 2-yard touchdown.

It was all Buffalo for the rest of the first half and much of the game. Allen hit Beasley for a 5-yard touchdown and later found tight end Knox for a 4-yard score as the Bills pushed ahead 17-7 by halftime.

Mullens finished 26-of-39 passing for 316 yards, three touchdowns and two interceptions.

PRIME-TIME SUCCESS

The Bills got a rare prime-time win. They're now 8-22 in prime-time games since 2000, and 2-8 on Monday night, with their one victory coming against the Jets in November 2014, when the game was moved back a day and played in Detroit after a major snowstorm snarled much of Buffalo.

It was their first win in an official “Monday Night Football” game since beating Miami 23-18 in 1999.

ALLEN'S HOMECOMING, SORT OF

Allen had a big game against the 49ers, which is the team he rooted for while growing up in Firebaugh, California. The town is about a 3-hour drive from San Francisco, and Allen went to games as a kid with his family. This was the first time he had played against San Francisco.

INJURIES

Bills: Beasley left in the first quarter and was evaluated for a head injury but quickly returned. ... RB Devin Singletary (knee) left in the first quarter but returned. ... Safety Jaquan Johnson was hurt on a punt return in the fourth quarter and limped off the field with help.

49ers: DT D.J. Jones (ankle) left in the first half and didn't return.

UP NEXT

The Bills host the Pittsburgh Steelers on Sunday night.

The 49ers stay in Arizona and they'll host Washington on Sunday.