South Africa lost seven wickets for 33 runs in a collapse and Pakistan's batsmen also struggled as the ball swung and seamed late on the first day of the final test at the Wanderers on Friday.
South Africa was 154-2 and 229-3 before Pakistan's quick bowlers carved through the middle and lower order in the final session to bowl the home team out for 262 and give the tourists hope of avoiding a 3-0 series whitewash.
Pakistan is 2-0 down and has already lost the three-match series but team changes and a helpful pitch seemed to invigorate its bowling lineup.
Allrounder Faheem Ashraf took 3-57 after being one of three players called into the side for the series-ender. Mohammad Abbas, Mohammad Amir and Hasan Ali, another bowler brought in, had two wickets each as the Pakistan attack worked together to give the touring team its best day of the series.
Pakistan's reply at stumps tempered that slightly, with Shan Masood and Azhar Ali falling off consecutive deliveries from Vernon Philander. Pakistan used Abbas as a nightwatchman to fend off any more damage and was 17-2 at stumps, 245 behind.
Twelve wickets fell on the first day, nine of them in the eventful last session.
Pakistan's fightback was extraordinary after South Africa opener Aiden Markram made 90 78 of those runs in the first session and was part of a 126-run partnership for the second wicket with Hashim Amla (41).
Theunis de Bruyn (49) and Zubayr Hamza (41), who was on his test debut, had a 75-run stand for the fourth wicket to keep South Africa in apparent control.
It seemed that Pakistan was going to rue not capitalizing on an early breakthrough, when stand-in captain Dean Elgar was out for 5 in the second over and South Africa was 6-1. Elgar is captaining South Africa after Faf du Plessis' one-test ban for over-rate offenses in the first two matches.
But de Bruyn's dismissal, lbw to the impressive Abbas to make it 229-4 straight after tea, sparked the collapse.
Amir grabbed two crucial wickets in quick succession, working over Temba Bavuma with a series of outstanding deliveries before the batsman edged behind. He also removed Hamza four overs later and the South African tail capitulated.
For South Africa, it was a case of missed opportunities. Markram, having hit 16 fours and played with complete control, let a century slip from his grasp when he edged down the leg side to a tame delivery from Faheem. Amla, de Bruyn, and Hamza all fell with 50 in their sights.
The Pakistan bowlers, second-best to South Africa's pace attack in the first two tests, got the ball to swing, and at times to reverse swing in the late afternoon. They found the Wanderers pitch to their liking, where getting the ball to move around brought more rewards than out-and-out pace.
Philander got the ball to swing and seam to good effect, too, and dismissed Masood and Azhar to edges behind to wicketkeeper Quinton de Kock in the second over he bowled.
Elgar called for a decision review for the first strike when umpire Sundaram Ravi gave Masood not out. The TV replays showed a faint edge.
Dean Elgar (captain), Aiden Markram, Hashim Amla, Theunis de Bruyn, Temba Bavuma, Zubayr Hamza, Quinton de Kock (wicketkeeper), Vernon Philander, Kagiso Rabada, Dale Steyn, Duanne Olivier
Imam-ul-Haq, Shan Masood, Azhar Ali, Asad Shafiq, Babar Azam, Sarfraz Ahmed (captain/wicketkeeper), Shadab Khan, Faheem Ashraf, Mohammad Amir, Mohammad Abbas, Hasan Ali