Man-of-the-match Habana scored a hat-trick - including two typical long-range efforts - with the Springboks winger's gamebreaking third coming in the 69th minute.
A penalty goal with three minutes left from his compatriot Morne Steyn sealed the victory and signalled scenes of jubilation amid the Nick Mallett-coached invitation side, dominated by Springboks and Wallabies and including 60-test All Black Joe Rokocoko.
It ended the All Blacks' six-match winning streak in the latter part of the season, and ended an up and down year with a record of 10 wins and five defeats.
Despite tries to Ben Smith and Anthony Boric, it was just the All Blacks' second loss to Barbarians in 10 matches, stretching back to their first encounter in 1954.
The crowd of 63,551 were treated to a fast, open spectacle on a dry ground, and while the second-string All Blacks gave the ball some air, they couldn't finish numerous chances.
Halfback Brendon Leonard and second five-eighth Luke McAlister impressed after quiet tours, while captain Richie McCaw was benched at halftime to allow Tanerau Latimer a chance.
The Baabaas' abrasive loose forwards Schalk Burger and Rocky Elsom were prominent figures in another highly physical encounter, while Rokocoko got few chances to show his wares.
It was one of the stronger Barbarians sides in recent memory, captained by Springboks colossus Victor Matfield, with eight other South Africans and six leading Wallabies in the 22.
And thanks to two opportunist Habana tries in the first half they led the All Blacks 14-10 at the break after the tourists made nearly all the play.
After Matfield caused some heart flutters by stealing the first lineout, Stephen Donald opened the scoring before Habana's first came from nowhere, 90m out.
After a strong Corey Flynn charge, the All Blacks cleaned out at the ruck, but fullback Drew Mitchell pilfered the ball and raced away, with his in-pass sending Habana on a clear run.
The All Blacks attacked at will but their early tour problem of not finishing chances returned, with several stray passes with overlaps beckoning.
Smith finally opened their account in the 24th minute after McAlister cut through, and simple catch and pass sent the Highlanders winger over for his first try in the black jersey.
The All Blacks had to defend strongly as the Barbarians probed, with McCaw and Leonard pulling off trysaving tackles.
Just when they looked poised for a halftime lead, Donald ended a mixed half with a stray pass which Habana juggled three times then raced 70m.
Coach Graham Henry made three changes at halftime, with Mike Delany replacing Donald, Tanerau Latimer replacing McCaw and Sitiveni Sivivatu replacing Tamati Ellison.
McAlister soon saved the All Blacks from further embarrassment with a huge trysaving tackle on Wallaby Rocky Elsom after he gathered a Matt Giteau crosskick.
The All Blacks still couldn't cross, with Leonard going closest before being chopped down by a groggy Habana two metres out.
The Barbarians could sense an upset, which showed when Giteau opted to kick for goal after a 56th minute penalty to extend the lead to 17-10.
Finally, the All Blacks scored their second 62nd minute when Boric burrowed over after good leadup work from McAlister, but Delany couldn't level the scores when he pushed his first attempt left.
It got desperate for the All Blacks as Zac Guildford was forced off, and with all their back reserves used, Liam Messam was forced onto the wing.