The tourists, who conceded a first-innings lead of 177 runs, survived the loss of an early wicket to go to tea on the third day at 129-1 in their second knock.
Shehzad was batting on 69 and Azhar was on 47, with Pakistan only 48 runs behind on the P. Sara Oval pitch that is expected to turn increasingly in favour of bowlers as the match progresses.
Sri Lanka, looking for a series-levelling win after losing the first Test in Galle, removed Mohammad Hafeez cheaply for eight before lunch, caught in the slips by Kumar Sangakkara off Angelo Mathews.
Shehzad and Azhar, who resumed after lunch at 62-1, settled down to defy the Sri Lankan bowlers through the post-lunch session. Shehzad has hit four boundaries and a six, while the watchful Azhar has two fours.
More From This Section
Sri Lanka, who began at their overnight score of 304-9 after a delayed start due to bad weather, were dismissed for 315 in their first innings in reply to Pakistan's 138.
Leg-spinner Yasir Shah finished with figures of six for 96 after last man Chameera was caught superbly by Younis Khan running back from the slips, leaving Rangana Herath unbeaten on 18.
The third and final Test starts in Pallekele on July 3.