Sri Lanka looked to be retaining control heading into the last day as they boasted a lead of 220 with still five second-innings wickets in hand, while only needing a draw to win the three-match series 1-0.
However, after taking the field on 133 for five, they were bowled out for 214 on Monday morning as Pakistan gave themselves a glimmer of hope of getting the win they need.
The momentum of the match changed, though, following the the dismissal of Mathews, caught by Khurram Manzoor at deep square leg off paceman Mohammad Talha for 31, which sparked a collapse that saw Sri Lanka lose their last five wickets for just 25 runs.
Spin duo Abdur Rehman (4-56) and Saeed Ajmal (3-53) haunted the Sri Lanka order by taking two wickets each, with the latter claiming the head of Jayawardene after the wicketkeeper had top-scored with 49.
Lunch immediately came following the fall of the final wicket of Shaminda Eranga to set up an exciting run chase with Pakistan having to win to level the series.