The Dane tumbled to the track in the penultimate scratch race before getting back in the saddle and clinching gold in a dramatic time-trial finale.
Hansen was tied in a three-way points lead with France's Bryan Coquard, who claimed the silver, and Italy's Elia Viviani heading into the showdown event but he edged out all his gold-medal rivals in the decider.
He ensured victory with a second place finish in the time-trial, with only Britain's Ed Clancy, who took bronze, finishing ahead of him in the 1-km dash.
His victory, however, was all the more impressive after he suffered a nasty crash in the fifth event, skidding on the pine before getting back on his bike to finish strongly.
"When I hit the ground I thought this is it is over," he said. "There is no chance, I am out of the medals.
"But then I didn't feel that bad. I knew I had to just finish as strong as I could."
Hansen collided with Clancy early in penultimate race, but the rules allowed him five laps to get back on his bike and rejoin the 15km ride.
While he was getting back up to speed, a breakaway group including Coquard and Viviani lapped the chasing pack but Hansen gave chase on his own to rejoin the leading group.
That set him up perfectly for the final time trial, finishing second behind Clancy, who produced a scintillating ride to climb from fifth to third in the standings.
"I came here for team pursuit gold. In the omnium I knew it was touch and go (to get a medal)," Clancy told reporters.
"I want to say thanks to the team, at one point I even had the great Mark Cavendish on the phone offering me some advice," he said in reference to the road race world champion and 23-time Tour de France stage winner.
Clancy, who won the team pursuit Olympic title in world record time with Steven Burke, Peter Kennaugh and Geraint Thomas, cracked in the scratch race, finishing 10th, and said that cost him silver or gold.
"I had the form of my life in the kilo (time-trial), the flying lap and the individual pursuit. I beat them by a mile in all of them," he said.
"At one point I was looking at getting the gold but, in the scratch, it just slipped away."