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 hopes when he took two wickets off successive balls in the third over of the morning.
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 fast, full delivery.
With Vernon Philander gaining 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 seamed away from him.
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 because Philander was injured, 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 man of the match.
(This story has not been edited by Business Standard staff and is auto-generated from a syndicated feed.)