Hafeez (80 not out) and debutant Shehzad (55) added 101 runs for the second wicket to take their team to safety after Sri Lanka paceman Suranga Lakmal dismissed opener Khurram Manzoor (eight) early.
Pakistan reached 158 for two wickets when both teams agreed to a draw with first-innings centurion Younus Khan (13 not out) the other unbeaten batsman at the crease.
Lakmal troubled Manzoor on numerous occasions with his outswingers before finding his edge for wicketkeeper Prasanna Jayawardene to complete the catch as Pakistan chased 302 for an improbable victory in just over two sessions.
Shehzad was out leg before to Rangana Herath at the start of the final session and his attempt to overturn the umpire's decision through a review also proved unsuccessful.
But the dismissal came a bit too late for Sri Lanka as both their spinners and fast bowlers failed to make any significant impact on a docile pitch.
Earlier, Sri Lanka declared their second innings on 480 for five wickets after batting for 90 minutes in the morning session to add 60 runs to their overnight score.
Captain Angelo Mathews remained unbeaten on 157, his highest test score, while Jayawardene was not out on 63. The duo added 156 runs for the unbroken sixth wicket.
The second of the three-test series will be played in Dubai from Wednesday.


Latest News from Sports News Desk