Pakistan were facing defeat at 36 for three wickets in their second innings at the close on the third day of the first test against Sri Lanka after being set a formidable winning target of 510 runs.
Sri Lanka, who scored 472 in the first innings, closed their second innings at 137-5, leaving Pakistan with a tough task of batting out 15 overs and the entire two days to save the test.
Pakistan, who were shot out for 100 in their first innings, lost wickets regularly during their run chase.
Nuwan Kulasekera once again broke through the Pakistan batting ranks to finish with two wickets for 13 runs.
He separated the openers when he had skipper Mohammad Hafeez edging a low catch to Mahela Jayawardene at first slip in the fifth over. Hafeez hit only a boundary in his innings.
Kulasekera struck again 10 minutes before the close when he bowled the other opener Taufiq Umar through the gate for 10.
Rangana Herath picked up the wicket of Azhar Ali for seven when he edged a catch to Thilan Samaraweera at second slip.
Younis Khan (0) and nightwatchman Saeed Ajmal (11) survived the remaining overs but Pakistan still required 474 more runs.
The hosts, having decided against enforcing the follow-on, lost three wickets in the afternoon session looking for quick runs.
Tharanga Paranavitana (25) and Tillakaratne Dilshan set the tone with an opening partnership of 81 off 20 overs before Saeed Ajmal separated them.
Ajmal won a leg-before appeal against Paranavitana and four runs later, Dilshan fell to Junaid Khan to depart after a 58-ball 56 that included nine fours.
Kumar Sangakkara, who was stranded on 199 not out in the first innings, made just one run before mistiming a drive to extra cover where Taufiq Umar held the catch to give Ajmal his second wicket of the innings.
Sri Lanka lost Mahela Jayawardene for 14 and Thilan Samaraweera for 15 after the tea break before applying the closure to their innings.
Earlier, spinners Herath and Suraj Randiv bowled Pakistan out for 100 at lunch after the visitors had resumed on 48 for five.
Pakistan's last five wickets lasted just a session as Herath (3-30) and Randiv (4-13) ran through the bottom order.
Left-arm spinner Herath made the first breakthrough of the day by trapping Younus Khan (29) leg before. Younus was clearly unhappy with the decision and television replays later showed him getting an inside edge on to the pads.
Compounding Pakistan's misery, debutant Mohammad Ayub and wicket-keeper Adnan Akmal were involved in a mix-up where both batsmen finished at the striker's end.
Akmal swept Herath to fine leg and set off for a run then called for a second but Ayub hesitated. Akmal found himself at the same end when the throw from Nuwan Pradeep came to Herath, who had plenty of time to take off the bails at the non-striker's end.
After Akmal departed for nine the end came swiftly. Randiv trapped Abdur Rehman lbw for one and finished off the innings when Umar Gul (two) made a big heave and the resulting top edge was caught by wicket-keeper Prasanna Jayawardene to give the bowler his fourth wicket of the innings.
Herath ended Ayub's innings at 25, trapping him lbw.