The rare win for Bangladesh -- just their fourth away from home and first against Sri Lanka in 18 attempts -- ended the two-Test series in a draw after the visitors endured a 259-run loss in the opener in Galle.
Chasing a target of 191, teenage batsman Mehedi Hasan hit the winning run after an anxious half hour in the final session on the fifth day, getting Bangladesh across the line in 57.5 overs.
Sri Lanka struck in phases through spinners Dilruwan Perera and skipper Rangana Herath, who grabbed three wickets each, but lacked the cutting edge to deny Bangladesh from celebrating their 100th Test match in style.
Bangladesh began looking nervous when Tamim -- who faced 82 balls and hit seven fours and a six -- hoisted a catch to Dinesh Chandimal off Perera just before the tea break.
More From This Section
Perera then removed Sabbir through a successful review after the bowler's initial appeal for a leg-before was rejected, giving Sri Lanka an outside chance for a win.
Mushfiqur remained unbeaten on 22 to take Bangladesh home with some support from debutant Mosaddek Hossain, who made 13 runs during the crucial period.
Skipper Herath earlier in the morning thought to have given Sri Lanka some control once he picked up two wickets in two balls to leave Bangladesh struggling at 38-2 at the lunch break.
Herath claimed his first wicket when opener Soumya Sarkar, charging down the pitch, was deceived by a turn and lofted a catch to Upul Tharanga at mid-off for 10.
Perera and Suranga Lakmal frustrated Bangladesh for more than an hour with the bat as Sri Lanka added 51 runs to their overnight 268-8 before finishing up for 319 in their second innings.
Perera hit his fourth Test fifty off 174 balls while tail-ender Lakmal smashed 42 off 48 balls. The duo shared 80 runs for the ninth wicket before Bangladesh separated them through a run out.