Seamer Hasan Ali took a career-best 10 wickets in the match as Pakistan blitzed South Africa with the new ball on Monday to complete a 95-run victory in the second and final Test in Rawalpindi and claim the series 2-0.
Chasing a target of 370 for victory, which would have been a Test record in Pakistan, South Africa were well placed at 241 for three just after lunch on the fifth day following a century from Aiden Markram (108).
But when the home side took the new ball, Hasan (5-60) and Shaheen Afridi (4-51) decimated the South African middle order with expert swing bowling to dismiss the visitors for 274.
Hasan took career-best figures of 10-114 in the match as South Africa lost their last seven wickets for the addition of only 33 runs.
It was yet another dismal batting collapse, something that has been a feature of their cricket home and away for the last few years, but on this occasion it was brought about by some high-class seam bowling.
"It was outstanding from Hasan because he has awareness and took advantage of the experience of playing here (in Rawalpindi)," Pakistan captain Babar Azam said at the post-match presentation.
"At one stage we thought the game could've gone either way. But we knew, one wicket and we can come back into the game again. And then we did that with the new ball."
The excellent Markram started the final day with Rassie van der Dussen (48) but the latter lasted only a few balls before he was bowled by Hasan, who then trapped the experienced Faf du Plessis (5) lbw shortly afterwards.
South Africa turned the tide with a superb stand of 106 for the fourth wicket between Markram and Temba Bavuma (61) that gave the potential for an upset win.
But when Pakistan captain Babar Azam took the new ball on exactly 80 overs, the game changed dramatically as the flat wicket came to life, and Hasan and Shaheen were able to extract prodigious movement.
Markram edged Hasan to Imran Butt at second slip, and the very next ball captain Quinton de Kock inexplicably flashed at a wide delivery and was caught by the same player for a duck.
It became a procession after that and the final nail in the coffin for the visitors came when Shaheen removed the patient Bavuma as he induced an edge to wicketkeeper Mohammad Rizwan.
"We lost the big moments in the game and that cost us" De Kock said. "When we get back home, we'll have to look at our performances and improve on the way forward."
South Africa are making a first visit to Pakistan since a militant attack on the Sri Lanka team bus in Lahore in 2009 that killed six policemen and two civilians.
A much-changed squad will now contest a three-game Twenty20 series that starts on Thursday.