Pakistan's spinners Yasir Shah and Nauman Ali grabbed three wickets in the space of 10 runs to halt South Africa's fightback on the third day of the first Test in Karachi on Thursday.
When it looked like South Africa would end the day at parity after wiping out Pakistan's big 158-run first-innings lead, Yasir struck twice and Nauman once to leave the tourists on 187-4 at close.
Nightwatchman Keshav Maharaj was unbeaten on two and skipper Quinton de Kock was yet to score, as South Africa now leads by 29 runs with six second-innings wickets in hand.
Earlier, Pakistan scored 378 to take a crucial lead over South Africa's first innings total of 220.
Opener Aiden Markram, who scored a polished 74 for his eighth 50, added 127 for the second wickets with Rassie van der Dussen who scored a solid 64.
But Yasir, with figures of 3-53, changed the scenario by dismissing Dussen caught and then trapped Faf Du Plessis leg-before for 10, while Nauman had Markram caught by a close-in fielder to trigger a three-wicket collapse in 33 balls.
Markram, dropped on 27 by Pakistan skipper Babar Azam off-spinner Yasir, also had a leg-before decision overturned on four.
Dussen also had a leg-before decision overturned on 56. Markram hit 10 boundaries and Dussen five.
Both Yasir and Nauman will be major threats for South Africa on a fourth day National stadium pitch which produced 14 wickets on day one, four on the second and six on the third.
South Africa was off to a confident start with Markram and Dean Elgar defying Pakistan's attack and was 37 without loss at lunch.
Elgar, in visible discomfort after being hit on the left hand by a rising delivery from pacer Shaheen Shah Afridi, was caught by diving wicketkeeper Mohammad Rizwan off Yasir for 29.
In the morning, Pakistan's tail added 70 runs to the overnight 308-8 to further frustrate South Africa, who was looking for quick wickets.
South Africa's pace spearhead Kagiso Rabada bowled Hasan Ali for 21 to reach 200 wickets in his 44th Test.
Rabada, 25, is the eighth South African to take 200 or more Test wickets.
Pakistan was 323-9 at that point, but Yasir and Nauman added 55 valuable runs for the final wicket.
Yasir, who scored a Test century in Australia in 2019, hit six and four fours in his 38 not out, while debutant Nauman scored three boundaries before falling for 24.
Rabada with 3-70 and spinner Maharaj, who took 3-90, was the pick of the South African bowlers.