The Bills blew a 25-point lead in the second half after going up 28-3 in the third quarter. Buffalo rectified that when Allen completed a 3-yard touchdown pass to Tyler Kroft with 15 seconds left.
The touchdown pass helped the Bills seal the game.
Allen finished 24-for-33 with 311 passing yards, four touchdown passes, a rushing touchdown and an interception. Kroft had four catches for 24 yards and two touchdown passes. Stefon Diggs and Lee Smith also had touchdown catches.
Cole Beasley led the Bills with six catches for 100 yards.
The Rams got 114 rushing yards and a touchdown from Darrell Henderson. Jared Goff was 23-for-32 with 321 yards, two touchdown passes and an interception. Cooper Kupp and Robert Woods each had a touchdown catch.
Buffalo has now won its first three games in consecutive seasons. The last time the franchise did that was in 1991 and 1992. It’s the third time the team has done that overall.
The Bills are now 3-0 and the Rams are 2-1.
The Associated Press contributed to this report.