Facing a huge 296-run first innings deficit after Pakistan were all out for 628, the hosts did not retreat into a defensive shell and counter-attacked instead through Tamim (138 not out) and Imrul (132 not out).
Tamim survived a strong leg before appeal off the first ball he received from Junaid Khan and Pakistan decided to review the decision after the appeal was turned down but could not get it overturned.
The left-hander did not look back and went on to hit his seventh Test century -- the most by a Bangladeshi batsman -- in 123 balls while Imrul helped himself to his third Test hundred.
Earlier Pakistan, resuming on 537-5, lost their last five wickets for 34 runs and were all out for their highest total against Bangladesh.
Both the overnight batsmen Asad Shafiq (83) and Sarfraz Ahmed (82) fell short of centuries at the Sheikh Abu Nasser Stadium.
Left-arm spinner Taijul Islam (6-163) was the most successful bowler for the hosts but the day belonged to the Bangladeshi openers who frustrated the Pakistani attack on a surface on which 1200-plus runs have been scored over four days with only 20 wickets taken.
Tamim hit four sixes and 13 boundaries in his entertaining knock, playing with aggression which occasionally bordered on recklessness.
Imrul had to keep wicket for more than 100 overs after skipper Mushfiqur Rahim injured his right hand but he did not let it affect his batting, hitting 15 boundaries and three sixes.
The previous highest stand for Bangladesh was the 267 for the fifth wicket between Mohammad Ashraful and Mushfiqur against Sri Lanka at Galle in 2013.
Scorecard
Bangladesh 1st innings: 332
Pakistan 1st innings (overnight 537 for 5)
Mohammad Hafeez c Mahmudullah b Shuvagata 224
Sami Aslam c Rahim b Taijul 20
Azhar Ali b Shuvagata 83
Younis Khan b Taijul 33
Misbah-ul Haq c Rubel b Taijul 59
Asad Shafiq c&b Shakib 83
Sarfraz Ahmed c sub (Das) b Shahid 82
Wahab Riaz b Taijul 0
Yasir Shah lbw b Taijul 13
Zulfiqar Babar st Mahmudullah b Taijul 11
Junaid Khan not out 0
Extras: (B-5, LB-8, W-3, NB-4) 20
Total: (all out; 168.4 overs) 628
Fall of wickets: 1-50, 2-277, 3-339, 4-402, 5-468, 6-594, 7-595, 8-617, 9-617.
Bowling: Rubel 22-3-82-0, Shahid 19-4-59-1, Taijul 46.4-4-163-6, Shuvagata 34-1-120-2, Shakib 37-4-146-1, Mahmudullah 4-0-30-0, Soumya 1-0-2-0, Mominul 5-0-13-0.
Bangladesh 2nd innings:
Tamim Iqbal not out 138
Imrul Kayes not out 132
Extras: (W-1, NB-2) 3
Total: (For no loss; 61 overs) 273
Bowling: Junaid 8-0-44-0, Zulfiqar 16-0-60-0, Hafeez 9-0-46-0, Wahab 13-2-57-0, Yasir 15-0-66-0.
Image: Imrul Kayes and Tamim Iqbal
Photograph: Christopher Lee/Getty Images