Pakistan openers Khurram Manzoor (14) and Ahmed Shehzad (five) survived six overs before stumps to take their team to 19 for no loss, after Sri Lanka declared their first innings on 428 for nine wickets.
Needing a draw to seal a series victory, the Sri Lankan batsmen appeared in no hurry to score runs on a slow turning pitch.
Resuming on their overnight score of 220 for five, Sri Lanka lost Prasanna Jayawardene (35) early when he hooked Mohammad Talha straight to Junaid Khan at deep fine leg.
Jayawardene added 73 runs with Mathews (91) for the sixth wicket.
Mathews, who scored 91 and 157 not out in the drawn first Test in Abu Dhabi, continued to frustrate Pakistan and added 112 runs with Perera (95) before he holed out to Ahmed Shehzad at deep cover off Junaid.
The left-arm paceman trapped Rangana Herath leg before with his next ball for his third wicket to raise Pakistan's hopes of wrapping up the innings quickly.
But Perera continued to defy Pakistan, adding 72 for the ninth wicket with tailender Shaminda Eranga (25) and take the total past 400.
He fell in similar fashion to Jayawardene, his hook shot off Talha finding Junaid in the deep.
Pakistan managed to keep a lid on the scoring rate but were left to rue their bad luck during the day with a number of close appeals going against them.
Scorecard
Sri Lanka 1st innings (overnight 220-5)
D. Karunaratne c Younis b Rehman 34
K. Silva c Sarfraz b Talha 17
K. Sangakkara c Manzoor b Junaid 52
M. Jayawardene c Ali b Ajmal 47
D. Chandimal c Shafiq b Ajmal 11
A. Mathews c Shehzad b Junaid 91
P. Jayawardene c Junaid b Talha 35
D. Perera c Junaid b Talha 95
R. Herath lbw b Junaid 0
S. Eranga not out 25
S. Lakmal not out 3
Extras: (b5, lb11, nb2) 18
Total: (nine wkts dec; 172 overs) 428
Fall of wickets: 1-31, 2-65, 3-125, 4-159, 5-166, 6-239, 7-351, 8-351, 9-423.
Bowling: Junaid 32-5-81-3, Talha 32-3-99-3, Ajmal 55-16-120-2, Rehman 50-14-101-1, Azhar 3-0-11-0.
Pakistan 1st innings
Khurram Manzoor not out 14
Ahmed Shehzad not out 5
Total: (for no loss; 6 overs) 19
Bowling: Herath 3-1-7-0, Lakmal 1-0-4-0, Perera 2-1-8-0.
Image: Angelo Mathews
Photograph: Mark Kolbe/Getty Images
121 Tamil Nadu fishermen released by Sri Lanka
Sri Lanka recall Jayawardene, Herath for Pakistan Tests
Pacy Sri Lanka send Pakistan crashing for 165 in second Test
Hafeez, Shehzad score fifties as first Test ends in draw
Younis, Misbah steady Pakistan but Sri Lanka in control