A back-and-forth affair looked headed for overtime before Ka'imi Fairbairn intervened.
The Texans kicker set the team's franchise record by scoring his 770th point in the third quarter, but he had bigger things planned on the day. With no time remaining on the clock, Fairbairn lined up a 59-yard field goal from the H logo at midfield - the first time an H has been featured on the Texans field.
The kick twisted left before straightening out and splitting the uprights to give Houston their fourth win of the season.
Tyler Bass opened the scoring with a first quarter field goal but the Texans responded with a Cam Akers touchdown run off the left side.
It looked like the opening frame would end 7-3 in Houston's favor, but as the clock wound down to end the quarter, C.J. Stroud spotted Nico Collins sprinting past the defense and dropped a 67-yard touchdown into the receiver's hands.
Houston held Buffalo scoreless in the second quarter and Fairbairn extended the home side's lead with a 50-yarder with 30 seconds remaining in the half.
After an early drive put Fairbairn in position to set the Texans' franchise scoring record with a 47-yard hit, the Bills responded with a pair of touchdowns from James Cook and Keon Coleman, the latter on a long fourth down play.
Coleman's touchdown put the Bills within three of the Texans, and Bass tied things up just over a minute into the fourth quarter.
But after Houston pinned Buffalo deep in their own end and forced a punt, Stroud took the home side inside field goal range and made way for Fairbairn.
The hold was good and the kicker delivered. H-Town went wild.
The win takes the Texans to 4-1 on the season as they prepare for a two-game road trip to New England and Green Bay.
Houston is back home on October 27 when they welcome the Indianapolis Colts to NRG Stadium
View the best photos from the Week 5 matchup between the Houston Texans and Buffalo Bills.