Bangladesh were 555 for six when the match concluded after a rain-disrupted final day.
The hosts, who trailed by 296 runs after Pakistan's mammoth first innings 628, will take huge confidence into the second Test in Dhaka next week after rescuing a draw from what had looked like an almost hopeless position just a day earlier.
Tamim's 206 was the highest score by a Bangladeshi batsman in Test cricket while his stand with Imrul was the first triple century partnership for the country in the longest form of the game.
The pair resumed on 273, trailing the visitors by 23 runs, and added 39 more runs before the stand was finally broken when Imrul holed out in the deep off Zulfiqar Babar, one of seven bowlers Pakistan used as they tried to force a result.
Tamim retained the aggression that had brought him such success on Friday and brought up his double century in some style, marching down the wicket and smashing Junaid Khan high and straight for his seventh six.
Pakistan finally got their man when Mohammad Hafeez, who also scored a double century in Pakistan's innings, beat Tamim with spin and Sarfraz Ahmed stumped him.
In addition to the seven sixes, Tamim hit 17 fours in the 278-ball, 448-minute knock.
Bangladesh's lead was now in triple figures and Shakib Al Hasan pitched in with an innings of 76 not out to steer the home side safely towards stumps.
The previous best Test innings by a Bangladeshi batsman was 200, which Mushfiqur Rahim scored against Sri Lanka in Galle in 2013.
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.
Scoreboard:
Bangladesh 1st innings: 332 (M. Haque 80, I. Kayes 51)
Pakistan 1st innings: 628 (M. Hafeez 224, A. Shafiq 83, A. Ali 83, S. Ahmed 82, Misbah-ul-Haq 59; T. Islam 6-163)
Bangladesh 2nd innings (Overnight: 555-6)
T. Iqbal st S. Ahmed b Hafeez 206
I. Kayes c sub b Babar 150
M. Haque b J. Khan 21
Mahmudullah lbw b J. Khan 40
S. Al Hasan not out 76
M. Rahim lbw b Hafeez 0
S. Sarkar c Hafeez b Shafiq 33
S. Hom not out 20
Extras (lb-4 nb-3 w-2) 9
Total (for 6 wickets, 136 overs) 555
Fall of wickets: 1-312 I. Kayes, 2-345 M. Haque, 3-399 T. Iqbal, 4-463 Mahmudullah, 5-464 M. Rahim, 6-524 S. Sarkar.
To bat: T. Islam, R. Hossain, M. Shahid
Bowling: J. Khan 21-5-88-2 (nb-3 w-1), Z. Babar 32-1-125-1, M. Hafeez 20-0-82-2,W. Riaz 20-3-75-0 (w-1),Y. Shah 30-2-123-0, Az. Ali 6-1-26-0, A. Shafiq 7-0-32-1.
Image: Tamim Iqbal
Photograph: Julian Herbert/Getty Images