Pakistan were staring defeat in the second Test in the face after South Africa out-batted them and then bowled well on the third day in Dubai on Friday.
South Africa, seeking a series-levelling win, once again rocked Pakistan's top order with three early wickets after compiling a mammoth 517 in their first innings for a big 418-run lead at Dubai stadium.
At the close, Pakistan captain Misbah-ul Haq (42) and Asad Shafiq (28) were at the crease but they still need a whopping 286 runs to avoid an innings defeat or bat out two days to force a draw.
The duo have added 62 for the fifth wicket and would need a Herculean effort to avoid defeat.
But South Africa's bid for victory, and keeping their seven-year unbeaten away series record, was tainted when they were docked five penalty runs by the onfield umpires after television replays showed Faf du Plessis apparently trying to tamper with the ball in Pakistan's 31st over.
The two umpires, Rod Tucker of Australia and Ian Gould of England, called South African captain Graeme Smith for a warning, changed the ball and added five penalty runs to Pakistan's total.
Under International Cricket Council rules, if the umpires lay charges against any player match-referee David Boon will summon the player.
But even that South African setback couldn't save Pakistan as they continued to slump after finding themselves on two for two at lunch.
Dale Steyn gave South Africa an ideal start as he removed Shan Masood with the fourth ball of the innings, having him trapped in front of the wicket with a sharp delivery for nought.
It became two for two when Khurram Manzoor fell for his second duck of the match, caught off a miscued drive off Vernon Philander from the fifth ball of the second over.
Manzoor had made 146 in Pakistan's seven-wicket in last week's Abu Dhabi Test.
Azhar Ali and Younis added 46 for the third wicket before part-time spinner Jean-Paul Duminy struck with his first ball, trapping Ali lbw with a low delivery for 19.
Duminy could have taken Younis on 28 but Kallis failed to hold a low catch in the slips.
Younis was finally bowled for 38 by leg-spinner Imran Tahir off a strange shot, who took a career-best 5-32 to dismiss Pakistan for a paltry 99 in the first innings.
In the morning session, South Africa lost their last six wickets for the addition of 57 runs after resuming at 460-4. Off-spinner Saeed Ajmal finished with 5-161 while paceman Mohammad Irfan took 3-102.
De Villiers (164) was the first man to go, caught behind off Irfan in the fifth over of the day.
He hit 17 fours and a six during a commanding 376-minute innings and completed an all-time South African fifth wicket record partnership of 338 with Smith.
Smith was finally caught in the slip by Younis off Ajmal.
Smith's superb knock lasted 10 hours and 32 minutes during which he dominated Pakistan's bowling, hitting 16 fours and faced 388 balls.
With Smith's catch, Younis beat Javed Miandad's record of most Test catches of 93. It took him 86 Tests, 38 fewer than his illustrious countryman.
Irfan removed Duminy for seven before he was forced out of attack by Australian umpire Rod Tucker for running into the danger zone after a second warning.
Ajmal then wrapped up the innings with the last three wickets to complete his ninth five-wicket haul in Tests.