J.P. Losman hit Roscoe Parrish for a 30-yard completion to set up Rian Lindell's 42-yard field goal as time expired for Buffalo.
It was the second straight week Losman led a late winning drive. Last week, he hit Peerless Price for a 15-yard score with nine seconds left to beat Houston 24-21.
The Bills' Willis McGahee, in his first game back from broken ribs, scored on runs of 4 and 30 yards for his first two-touchdown game since the 2004 season finale.
And Parrish's 82-yard punt return with 3:46 left in the third put Buffalo up 24-14.
Josh Scobee cut the lead with a 27-yard field goal four seconds into the fourth. David Garrard then capped an 11-play, 62-yard drive with a 3-yard touchdown to Matt Jones with 28 seconds left. Two plays earlier, Garrard converted fourth and 14 with a 16-yard run, getting the final 4 yards with two defenders hanging from him.
Then Losman rallied the Bills. After a 6-yard completion to Parrish, he stepped up in the pocket and threw to the left sideline for Parrish, who kept both toes inbounds to make the catch just in front of Scott Starks.
"It was pretty cool," Losman said. "Sometimes you've got to let it go and let players make the plays. I don't think many receivers could've caught that like he did."
Video replays proved inconclusive, and after a Losman run to the middle of the field, Lindell split the uprights.
NOTABLE: The Bills won consecutive games for the first time since Oct. 9 and 16 2005. Since then, they were 7-13 with losing streaks of two, three and five games.