Duanne Olivier sparked a batting collapse as South Africa completed a series whitewash with a 107-run win on the fourth day of the third and final Test at the Wanderers Stadium.
Starting the day on 153 for three, Pakistan lost their remaining seven wickets for 120 runs to be bowled out for 273.
Olivier effectively ended Pakistan’s slight hopes when he took two wickets off successive balls in the third over of the morning.
Babar Azam received a quick, rising ball angled in towards his throat, which he gloved to wicketkeeper Quinton de Kock.
Pakistan captain Sarfraz Ahmed was bowled first ball. Seemingly expecting another short-pitched delivery from Olivier, he was deep in his crease when he had his off stump knocked back by a full length delivery.
With Vernon Philander extracting unpredictable bounce from a good length just outside off stump, Shafiq advanced down the pitch and was caught at second slip off a ball which moved away off the seam from him.
Shafiq had taken his overnight score of 48 to 65, made off 71 balls with 11 fours.
Faheem Ashraf and Mohammad Amir were both caught by Aiden Markram at gully off Kagiso Rabada.
Shadab Khan batted well to make 47 not out and received support from Hasan Ali, who made a quick 22, and Mohammad Abbas until Abbas was run out after a mix-up.
Olivier, who got his chance to play in the series with an injury to Vernon Philander, was the leading wicket-taker with 24 at an average of 14.71. He was named man of the series.
Quinton de Kock, who made 129 for South Africa in the second innings, was declared man of the match.