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 were 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 were 212 for seven with Oshada (102 not out) at the crease and with the visitors needing an unlikely 264 runs for victory.
Pakistan need just three wickets on Monday to win the series -- their first at home since the 2009 attack on the Sri Lanka side resulted in international cricket being suspended in the country.
Pakistan earlier declared their 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.
Dickwella praised Oshada's resistance.
"He is a very good batsman and has scored heavily at domestic level," said Dickwella of the opener, who struck 13 boundaries in his 266-minute knock.
"Had we had one more big partnership we could have saved this Test," he added.
Pakistan's head coach Misbah-ul-Haq praised his team's fightback.
"We were under pressure after conceding an 80-run lead in the first innings," said Misbah. "To come out and be in a winning position is satisfying and eased the pressure."
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.