Pakistan could have had Mathews on 9 but Khurram Manzoor dropped a simple catch of the Sri Lanka captain's loose drive off Junaid Khan in the cover with Sri Lanka leading by 198 runs.
The dropped chance could prove costly for Pakistan. In the first innings Talha had dropped a difficult catch off Mathews from his own bowling on zero before the Sri Lankan captain went on to score 91.
Wicketkeeper Sarfraz Ahmed could not hold onto a legside catch off Prasanna Jayawardene's gloves before the batsman had scored to deny Ajmal his second wicket.
"Obviously we built a partnership and increased our lead so that we are out of danger, the pitch is taking turn but it's not very sharp," Sri Lanka's fast bowler Shaminda Eranga, who earlier took career-best 4-60, said.
Pakistan had a sniff when Talha clean bowled Chandimal in the first over after tea to reduce Sri Lanka to 89-4.
But Mathews and Jayawardene dug in and added 38 runs at a snail's pace with Pakistan spinners getting slow turn off the wicket.
Rehman tied down the scoring rate in Pakistan's most productive session after lunch as Sangakkara tried to cut loose in frustration but top-edged a sweep in the deep to Manzoor.
Rehman came close to dismissing Jayawardene lbw on 7, as Pakistan went for the television referral only to see that the ball would have missed the leg stump.