Oshada Fernando struck a defiant maiden international century but Pakistan need only three wickets to seal a Test series win over Sri Lanka heading into the final day in Karachi.
The hosts had piled on the runs in the first half of Sunday's play, Azhar Ali (118) ending a year-long wait for a Test century and the classy Babar Azam (100 not out) hitting a third in his last four matches after Abid Ali and Shan Masood had reached three figures on day three.
It was only the second time in history the top four have all made centuries in the longest format, enabling Pakistan to amass a mammoth 555-3 declared at the National Stadium.
The tourists were set an unlikely 476 to secure a 1-0 win in the first Test series to be played in Pakistan for a decade, but they were on the ropes at 212-7 at stumps.
Opener Oshada was unbeaten on 102 after he and Niroshan Dickwella (65) put on 104 for the sixth wicket, but Haris Sohail and the excellent Naseem Shah (3-31) struck late in the day as Pakistan closed in on victory.
Azhar set about Vishwa Fernando with a flurry of boundaries and celebrated his 16th Test century – albeit his first as captain - after Pakistan resumed on 395-2 in batting-friendly conditions.
The home team's skipper, who struck 13 fours in a brilliant knock, was finally on his way when he was stumped coming down the track to Lasith Embuldeniya, ending a third-wicket stand of 148.
Babar hit Lahiru Kumara for three boundaries in a row and the declaration arrived after he had recorded a ton for the second time in the series.
Sri Lanka were in trouble on 40-2 when 16-year-old Naseem had Kusal Mendis taken by Babar at third slip without scoring just one run after captain Dimuth Karunaratne had edged Mohammad Abbas behind.
Shaheen Afridi got in on the act by dismissing Angelo Mathews before tea, but Oshada stood firm and was rewarded with a half-century early in the evening session.
A fourth-day finish looked a possibility after Dinesh Chandimal and Dhananjaya de Silva fell cheaply, but Oshada and Dickwella refused to accept defeat.
Oshada had a life when he was put down by Naseem at backward point on 72 off Yasir Shah, while Dickwella was operating in one-day mode at the other end.
The wicketkeeper-batsman found the ropes 11 times in an entertaining knock before he was bowled by Haris attempting a reverse sweep. Naseem got rid of Dilruwan Perera just before the close too, not long after the resolute Oshada had celebrated reaching his hundred.