Azhar Ali was with Hafeez on 12, after debutant opener Sami Aslam was out for 20 at 50-1.
Left-arm spinner Taijul Islam broke the opening partnership as it looked threatening. Hafeez and Aslam shared 50 off 67 balls until Aslam grazed a leg-side delivery and was caught behind. The umpire gave not out, but Bangladesh won the appeal on video review.
Hafeez, who won a video review on 13, reached his own 50 off 67 balls, getting to his 11th test half-centuy in style by whacking Shakib Al Hasan over the midwicket fence. His innings also included five boundaries.
Bangladesh resumed on 236-4 and was bowled out for 332 in its first innings three overs after lunch.
Fast bowler Wahab Riaz and legspinner Yasir Shah claimed three wickets apiece. Hafeez and Zulfiqar Babar took two wickets each.