Shoaib took four for 26 as South Africa were bowled out for 124, their lowest total against Pakistan.
But Makhaya Ntini hit back for South Africa, taking four for 18 as Pakistan stumbled to 135 for six at the close Friday.
Pakistan were looking in control with Younis Khan playing a solid anchor innings before he fended a catch to gully off Ntini in the penultimate over of the day.
Then Kamran Akmal, who shared a 56-run stand with Khan, was out four balls later when he hooked Andre Nel to deep square leg.
Pakistan captain Inzamam-ul-Haq did not bat because of a sore shoulder but will bat Saturday.
Shoaib prised open the South African batting by taking the first two wickets and was a constant threat, bowling at high pace and exploiting a pitch which offered him steep bounce. He was recorded at speeds of up to 150kmh.
Leg-spinner Danish Kaneria took three for 36 and Mohammad Asif two for 34 as South Africa, leading the three-match series 1-0, crumbled.
Their previous lowest total against Pakistan was 214 at Faisalabad in 1997/98.
Shoaib sparked the South African collapse when AB de Villiers tried to hook his 16th delivery and edged the ball to wicketkeeper Kamran Akmal. Then he had Hashim Amla caught down the leg side off a glove.
Since his previous Test appearance against India in January last year, Shoaib, 31, has suffered from ankle and knee injuries and was banned from international cricket for using performance-enhancing drugs.
He was cleared by a Pakistani tribunal last month and joined the team in South Africa as cover for Umar Gul, who is recovering from an ankle injury.
Astonishingly, on a pitch which did not appear to have any demons, Pakistan put down four chances, none of which proved costly.
In contrast to the first Test, which South Africa won by seven wickets, Pakistan fielded a potent four-man bowling attack, with Shoaib and Mohammad Sami replacing Shahid Nazir and Rana Naved-ul-Hasan.
Kaneria came into the attack when Shoaib was rested after taking two for 17. Smith charged down the pitch and slammed the spinner's fourth ball past mid-off for four but was out for 28 off the next ball when he was deceived by a googly.
Akmal could not hold the chance but it bounced up for Younis Khan to catch it at slip.
Ashwell Prince was dropped by Akmal off Sami on two but was out four balls later, caught at first slip.
Kaneria had Jacques Kallis dropped at slip by Younis on 11 and Gibbs was dropped by Akmal off the next ball, only to miss a sweep off the next delivery to be leg before.
Kallis fell for 24 in Shoaib's second spell soon after lunch.
Mark Boucher made the top score for South Africa with 35 before he top-edged an attempted sweep off Kaneria to slip.
Ntini dismissed both openers before tea, with Imran Farhat edging a catch to third slip and Mohammad Hafeez caught off a glove at short leg when he tried to hook the fast bowler.
Three balls after tea Ntini had Yasir Hameed caught at third slip.
Mohammad Yousuf, who set a world record for most runs in a year in 2006, stabilised the innings in partnership with Younis Khan as they put on 60 for the fourth wicket.
Yousuf made 32 before he was leg before wicket to Shaun Pollock, who earlier had taken a painful blow on the top of his left foot while batting against Shoaib.
Pollock did not go out to field at the start of the Pakistan innings but tried his injured foot out during the tea break and bowled a tight seven-over spell, taking one for 23.
South Africa Ist innings 124 ( Mark Boucher 35, Graeme Smith 28, Jacques Kallis 24; Shoaib 4 for 36, Kaneria 3 for 36, Asif 2 for 34, Sami 1 for 15) Pakistan Ist innings 135 for 6 ( Younis Khan 45, Kamran Akmal 33, Mohd Yousuf 32; Ntini 4 for 18, Pollock 1 for 23, Nel 1 for 36)