Patrick Devenny's one-handed grab from 22 yards out tied it and Aric Goodman's conversion sailed through to put the Buffaloes ahead by a point with 2:04 left.
After defensive back Anthony Perkins picked off Jerrod Johnson's pass at midfield, the Aggies forced a punt, but Terrence Frederick muffed it and Buffs linebacker Marcus Burton recovered with 23 seconds to ice it.
Rodney Stewart ran 20 times for 118 yards and two touchdowns and Markques Simas caught seven passes for 135 yards for Colorado (3-6, 2/3 Big 12), which got the ball back with 3:53 left trailing 34-28.
Hansen had a delay flag and then was sacked for the seventh time, bringing up a second-and-25 from his 24. He was nearly dumped again but got off a shovel pass for 9 yards, then hit Simas for 45 yards to the Aggies' 22. On the next snap, he threw over the middle to Devenny for the score.
Then, their defense and special teams came up big.
The Aggies (5-4, 2-3) looked like they were about to become bowl eligible when Cyrus Gray, whose 99-yard touchdown on a kickoff return helped them take a 21-10 halftime lead, returned a kickoff 45 yards to midfield in the fourth quarter. Cornerback Jalil Brown shoved him late out of bounds, tacking on another 15 yards.
That allowed the Aggies to start their drive at the Colorado 40-yard line, but they stalled at the 3 and settled for Randy Bullock's 20-yard field goal that made it 34-28. That left 3:59 on the clock and the Buffs within a touchdown.
It was plenty for Hansen, who threw for 271 yards and ran for 45 more. His running yards would have been higher, but he lost 60 yards in sacks.
The Buffaloes had been reeling with coach Dan Hawkins on the cusp of his fourth-straight losing season and the departure during the week of sophomore tailback Darrell Scott, his prized recruit.
Some fans in the student sections at Folsom Field wore powder blue instead of gold in protest of the program's slide into mediocrity. It was the color the team wore from 1981-84 when they went 10-34 overall during one of the worst stretches in school history.
But those same fans were among those who stormed the field afterward.
The Buffs scored 11 points in the third quarter to tie it at 21, but the Aggies answered with 10-straight points for a 31-21 lead.
Demetrius Sumler's 7-yard TD run cut the Aggies' advantage to 31-28 with 7:14 remaining.
Von Miller had two of the Aggies' eight sacks, giving him 15 1/2, tops in the country.
Revisiting the history behind the Saban-Fisher relationship
Ryan McGee details the relationship between coaches Nick Saban and Jimbo Fisher ahead of their matchup on Saturday.
Out of Pocket highlights South Carolina's Jackson
Out of Pocket's Alyssa Lang discusses the Gamecocks' Director of On-Campus Recruiting, Jessica Jackson, and details her importance.
No. 12 Oregon on 4-game win streak, travels to face Arizona
Arizona's Jayden de Laura had one of the best passing games in school history against one of the Pac-12's worst teams last week.
Texas QB Quinn Ewers to return vs. Oklahoma, sources say
Texas QB Quinn Ewers, who has missed the past month with a significant injury in his non-throwing shoulder, is expected to start vs. the Sooners, sources say.
Cardinals, Cavaliers clash seeking first conference victory
Louisville (2-3, 0-3 ACC) at Virginia (2-3, 0-2), Saturday, 12 p.m. ET (ACCN)
Why bettors should lay the points with Alabama
Joe Fortenbaugh and Stephen A. Smith expect Alabama to bury Texas A&M with ease.