Chasing 265 for victory, Steve Smith's Australia looked set to win on the penultimate day of the first Test after David Warner hit 112.
Australia were still favourites at 158-3, but lost their last seven wickets for 73 runs.
Bangladesh had failed to win their previous five Tests against Australia.
The hosts gained Test status in 2000 and are ninth in the ICC Test rankings - five places below Australia.
Australia lost five wickets in an eventful morning session on Wednesday before Shakib bowled Glenn Maxwell with the first delivery after the lunch break.
Maxwell was Shakib's 10th wicket of the match, the spinner, 30, having earlier become the fourth bowler to take five wickets in an innings against every Test nation.
Pat Cummins scored an unbeaten 33 down the order to inject some drama but it was not enough as Australia were all out for 244.
Shakib (5-85) celebrated his 50th Test appearance by scoring 84 in the first innings, a performance worthy of his status as the world's top-ranked Test all-rounder.
The second and final Test in the series starts in Chittagong on 4 September (05:00 BST).
Australia will be without fast bowler Josh Hazlewood, who suffered a side strain on day three in Dhaka and is set to miss Australia's limited-overs tour of India.
He is the third Australian paceman to be sidelined less than three months before the Ashes series against England, with Mitchell Starc and James Pattinson also injured.