Matthew Stafford completed 29 of 41 passes for 240 yards and one touchdown for the Rams.
Cooper Kupp caught 13 passes for 128 yards and one touchdown for the Rams.
The Bills made 413 yards and won despite committing four of the game's seven turnovers.
Allen connected on 26 of 31 passes, with two of the five incompletions intercepted. He also rushed for a game-high 56 yards.
Buffalo scored 21 unanswered second-half points to knock out Los Angeles.
The Bills took a 17-10 lead when Allen tossed a 7-yard touchdown pass to McKenzie with 7:59 left in the third quarter.
On the first play of the final quarter, Allen spotted Davis with 1-on-1 coverage for 47 yards to the Los Angeles 6-yard line. Three plays later, Allen scrambled to score from the 4, reaching the ball over the goal line with 13:27 left.
Four minutes later, the Bills used a long strike to increase their lead to 31-10. Diggs ran a streak down the right sideline, blowing past the Rams' Jalen Ramsey to haul in a 53-yard scoring reception.
Any Los Angeles comeback bid was short-circuited by Basham's interception with 6:58 left.