Shehzad, who was hit on the helmet by a Dushmantha Chameera bouncer before reaching his half-century, was unbeaten on 69 at with Ali (47 not out) proving a perfect foil.
Earlier, Pakistan had claimed the last Sri Lankan wicket in the morning session to bowl out the hosts for 315 with leg-spinner Yasir Shah (6-96) dismissing Chameera some 15 minutes after a rain-delayed start.
Younus Khan held a good running catch to send back Chameera as Sri Lanka, pressing for a series-levelling victory, took a 177-run first innings lead.
Shot out for 138 in their first innings, Pakistan did not make a great start in their second either as a leaden-footed Mohammad Hafeez (eight) poked at an Angelo Mathews delivery to be caught by Kumar Sangakkara in the slips.
Shehzad and Ali proved there were no demons in the pitch and they frequently used their feet to negate the threat posed by Sri Lanka's left-arm spinner Rangana Herath.
Shehzad overcame the blow on his helmet to bring up his fifty in the 35th over and Pakistan reached the 100-mark in the same over.
Original source: Shehzad fifty steadies Pakistan