Sam Bradford threw a last-minute touchdown to seal a late 15-12 win for the St Louis Rams.
Buffalo Bills led 6-0 at half-time through two Rian Lindell field goals in a game where defense was strong on both sides.
Steven Jackson gave the Rams the lead with a one-yard run early in the third quarter, but Bills tight end Lee Smith restored the home side's lead five minutes later.
The Bills attempted a two-point conversion to open up a seven-point lead, but Ryan Fitzpatrick's pass failed.
Brandon Gibson caught Bradford's touchdown pass with 54 seconds left on the clock to put the Rams ahead.
St Louis also went for a two-point conversion, which left the Bills needing a field goal.
The win leaves the Rams third in the NFC West, while the Bills are third in the AFC East.