Aiden Markram hit a solid half-century and Rassie van der Dussen gave him able company to give South Africa hope of an improbable and intriguing win in the second Test in Rawalpindi on Sunday.
Set a daunting 370-run target, South Africa finished the fourth day on 127-1 with Markram on 59 and Dussen unbeaten on 48. The tourists will need another 243 runs to level the series on Monday.
Earlier, Pakistan were helped to 298 all out in their second innings by a scintillating maiden hundred by wicketkeeper-batsman Mohammad Rizwan who finished on 115 not out.
South Africa had lost opener Dean Elgar to fast bowler Shaheen Shah Afridi for 17 at the total of 33, before Markram and Dussen negated Pakistani spinners on a seemingly unresponsive pitch, having added 94 for the unbroken second-wicket stand.
Markram, who did not score off his first 22 balls, changed gears to reach his fifth fifty off 71 balls and has so far hit nine boundaries and two sixes.
Dussen has so far hit eight boundaries.
South Africa can take confidence from the fact that two targets of 300 plus have been chased in the last 20 days with India overhauling 324 against Australia in Brisbane on January 18 and West Indies scoring 395 against Bangladesh at Chattogram on Sunday -- both on the last day.
Pakistan can take heart from the fact that no team has chased more than the 220 scored by Sri Lanka in 2000 in ten Tests in Rawalpindi, and that South Africa have not chased 250-plus since 2014.
It was Rizwan who gave Pakistan a sniff of victory.
He was at his best, putting on a match-turning 97-run partnership for the ninth wicket with number ten batsman Nauman Ali who made 45, frustrating the South Africans who were looking for a series-levelling win.
Rizwan, dropped on four by Dussen off Keshav Maharaj on Friday, punished South Africa and quashed their hopes of a win with 115 not out, spread over 292 minutes and laced with 15 boundaries.
He took a sharp single to reach his hundred, surpassing his previous best of 95 scored against Australia at Brisbane in November 2019.
The 97-run stand is also a new record for the ninth wicket against South Africa, bettering the 80 runs scored between Azhar Mahmood and Shoaib Akhtar at Durban in 1998.
Spinner George Linde, who took 5-64, recorded his first five-wicket haul when he dismissed the last man Shaheen for four. Maharaj finished with 3-118 while Kagiso Rabada had figures of 2-34.
Pakistan, resuming on 129-6, added 88 runs for the loss of Hasan Ali (five) and Yasir Shah (23) in the first session.
Hasan was the first to go when he was trapped leg-before in the sixth over of the day but Yasir helped Rizwan put on 53 for the eighth wicket in a stubborn stand.
Linde, who dropped Yasir off his own bowling when the batsman was on 15, finally had him caught behind by wicketkeeper Quinton de Kock to break the stand.
Scoreboard
Scores at close on the fourth day of the second Test between Pakistan and South Africa played at Rawalpindi stadium on Sunday:
Pakistan 1st innings 272 (Babar Azam 77, Faheem Ashraf 78 not out; A. Nortje 5-56, K. Maharaj 3-90)
South Africa 1st innings 201 (T. Bavuma 44; Hasan Ali 5-56)
Pakistan 2nd innings (overnight 129-6)
Imran Butt lbw b Rabada 0
Abid Ali c de Kock b Maharaj 13
Azhar Ali lbw b Linde 33
Babar Azam lbw b Maharaj 8
Fawad Alam c Markram b Linde 12
Mohammad Rizwan not out 115
Faheem Ashraf c Nortje b Linde 29
Hasan Ali lbw b Maharaj 5
Yasir Shah c de Kock b Linde 23
Nauman Ali c Elgar b Rabada 45
Shaheen Shah Afridi b Linde 4
Extras: (b9, nb2) 11
Total: (102 overs, all out) 298
Fall of wickets: 1-0 (Butt), 2-28 (Abid), 3-45 (Azam), 4-63 (Azhar), 5-76 (Alam), 6-128 (Ashraf), 7-143 (Hasan), 8-196 (Yasir), 9-293 (Nauman)
Bowling: Rabada 14-3-34-2, Nortje 17-7-57-0, Maharaj 38-4-118-3, Mulder 7-1-16-0 (2nb), Linde 26-9-64-5
South Africa 2nd innings
A. Markram not out 59
D. Elgar c Rizwan b Shaheen 17
R. van der Dussen not out 48
Extras: (nb3) 3
Total: (41 overs; for one wicket) 127
Fall of wicket: 1-33 (Elgar)
Still to bat: F. du Plessis, T. Bavuma, Q. de Kock, W. Mulder, G. Linde, K. Maharaj, K. Rabada, A. Nortje
Bowling: Shaheen 10-4-22-1 (nb3), Hasan 5-0-36-0, Nauman 12-4-35-0, Yasir 11-3-21-0, Ashraf 3-1-13-0
Toss: Pakistan
Umpires: Aleem Dar (PAK) and Ahsan Raza (PAK)
TV umpire: Asif Yaqoob (PAK)
Match referee: Mohammad Javed Malik (PAK)