Sri Lanka opener Oshada Fernando struck a maiden century to delay Pakistan's victory quest after the home team's top four all scored hundreds in the second Test in Karachi on Sunday.
Set a daunting 476 runs to win, Sri Lanka was tottering at 97 for five before Oshada and Niroshan Dickwella halted the slide with a resolute 104-run stand for the sixth wicket.
But the last three overs of the day saw Dickwella fall for 65 -- bowled while attempting a reverse sweep off spinner Haris Sohail -- and Dilruwan Perera caught behind off Naseen Shah for five.
The 16-year-old Naseem had the best figures of 3-31.
At the close, Sri Lanka was 212 for seven with Oshada (102 not out) at the crease and with the visitor needing an unlikely 264 runs for victory.
Pakistan needs just three wickets on Monday to win the series -- its first at home since the 2009 attack on the Sri Lanka side resulted in international cricket being suspended in the country.
Pakistan earlier declared its second innings at lunch on 555 for three after Azhar returned to form with 118 and Babar Azam struck 100 not out.
It was a dismal batting show by the Sri Lankans on a National Stadium pitch where the home batsmen flourished.
Skipper Dimuth Karunaratne went for 16, Kusal Mendis for a duck and Angelo Matthews for 19 before tea.
Pace spearhead Mohammad Abbas had Karunaratne caught behind in the tenth over of the innings before Naseem had Mendis snapped up in the slips.
It became 70 for three when Mathews, Sri Lanka's most experienced batsman, edged a short delivery from Shaheen Shah Afridi to wicketkeeper Mohammad Rizwan.
After tea, young Naseem accounted for Dinesh Chandimal (two) and leg-spinner Yasir Shah dismissed Dhananjaya de Silva (nought) to leave Sri Lanka struggling at 97 for five.
In the morning Pakistan batsmen flayed the Sri Lankan bowling.
It was only the second time in a Test innings that the top four all scored centuries after openers Abid Ali (174) and Shan Masood (135) grabbed theirs on Saturday. India against Bangladesh at Dhaka in 2007 was the other occasion.
Azhar, who had a lean 2019, completed his 16th century before he was stumped off spinner Lasith Embuldeniya after 11 boundaries in his 157-ball innings.
Azam's fourth century -- and third in the last four Tests -- had seven boundaries and a six off 131 balls.
Earlier, when Pakistan resumed at 395 for two, Azhar smashed three boundaries off pace bowler Vishwa Fernando and then took two runs to complete his first century in 12 months.
Azhar added 148 for the third wicket with Azam as Sri Lankan bowlers toiled hard for wickets. Lahiru Kumara finished with two for 139.
- Asian Games 2023: With packed arena and high ticket prices, Esports a hit in Hangzhou
- Leander Paes first Asian man to be nominated for International Tennis Hall of Fame
- Asian Games 2023: Postman by day, Esports player by night, Ayan Biswas takes flight at Hangzhou
- ISL 2023-24: Mohun Bagan SG meets Bengaluru FC in replay of previous edition’s title clash
- Indian sports news wrap, September 26