Pakistan is on course for a sizeable lead with captain Misbah-ul-Haque and Asad Shafiq on 43 and 6 runs, respectively.
Bangladesh was bowled out for 332 in its first innings. Hafeez achieved his maiden double century after it eluded him in his previous test against New Zealand, when he made 197.
The opener hit 23 fours and three sixes on his 332 balls before he was dismissed by off-spinner Shuvagata Hom while trying to sweep him.
Ali scored a patient 83 off 177, hitting four fours and one six before he became the first victim of Hom. Khan made 33 and looked set for a big innings until he played a lazy shot off left-arm spinner Taijul Islam to give his wicket.
Misbah showed an urgency to increase the run rate as he scored runs fluently. He hit two fours and as many sixes in his unbeaten 43 off 74 by tea.
Shuvagata Hom (2-76) and Taijul Islam (2-90) took two wickets apiece.
