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.
After surviving the Les Miles telenovela, which landed LSU's big cigars at No. 5, we're left to ponder who can claim the Bottom 10 title.
ESPN senior college football writer Mark Schlabach explains why Alabama defensive coordinator Kirby Smart is a good choice to replace Mark Richt as the coach at Georgia.
1H (0:36) PITT Michael Young made Dunk. Assisted by James Robinson.
1H (1:44) PITT James Robinson made Three Point Jumper. Assisted by Sterling Smith.
Alabama Crimson Tide defensive coordinator Kirby Smart is expected to be named the new head coach at Georgia, sources confirmed to ESPN's Mark Schlabach.