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.
GameDay 100 Leonard Fournette trucks over opponent to score 22-yard rushing touchdown.
Joel Honigford is Michigan's fourth ESPN 300 commitment this month and could develop into a quality starter.
ESPN.com's Ivan Maisel calls into The Paul Finebaum Show to make early predictions for this season's College Football Playoff.
Hugh Kellenberger joins The Paul Finebaum Show to discuss Mississippi State's 2016 season.
Hugh Kellenberger calls into The Paul Finebaum Show to preview the Rebels' season opener against the Seminoles.
The Missouri Tigers will hope a still-stout defense can hold the fort will the offense finds itself before a rough October stretch.