Hafeez finished with 80 not out for his ninth Test half-century, his first in 11 innings, while Ahmed Shehzad notched his maiden fifty on his debut as Pakistan, set 302 runs in 67 overs, finished on 158-2.
Pakistan's Azhar Ali smashed a second century in successive matches on Wednesday to leave the final Test against Sri Lanka poised for an intriguing finish.
The second Test between Sri Lanka and Pakistan at the Sinhalese Sports Club in Colombo ended in a stalemate, despite a spirited attempt by the visitors to force a result.
Mahela Jayawardena, an understated batsman in a world that long ago surrendered to overstatement, treated England to another gentle batting masterclass with a second successive Test century to ensure Sri Lanka maintained a position close to equilibrium at the close of the first day of the second Test.
Off-spinner Suraj Randiv forced his way back into the Sri Lankan team for the upcoming two-match Test series against England, scheduled to commence from March 26 in the southern port town of Galle.
Kumar Sangakkara hit his eighth double hundred and Prasanna Jayawardene notched up a fighting century to thwart Pakistan's hopes of victory to salvage a draw for Sri Lanka in the first Test here on Saturday.
England will have expected to have problems removing a Jayawardene during this series, but that was likely to be Mahela not Prasanna.