Pakistan 291 and 146 for 3 (Younis 93*, Iqbal 44, Steyn 2-44) need 278 runs to beat South Africa 450 and 264 for 7 dec (Kallis 100*, Prince 45, Rehman 4-105)
Younis Khan's aggressive unbeaten 93 off 99 balls set up an intriguing final day's play in Karachi, with Pakistan requiring another 278 runs to win while South Africa need seven more wickets.
Riding on Jacques Kallis' second century of the match, South Africa set Pakistan a daunting target of 424. South Africa got off to a good start as Mohammad Hafeez dragged an innocuous Dale Steyn delivery back onto his stumps in only the second over of the innings.
But Younis started off in aggressive fashion, driving uppishly past point before leaning into an elegant cover drive. Although Salman Butt got out to a remarkable catch by Hashim Amla at short leg, the flow of runs failed to cease as Makhaya Ntini, who is yet to take a wicket in the match, was driven, flicked and caressed past wide mid-on for more boundaries by Younis.
As if not content with the conventional strokes, Younis, looking to decrease as much of the deficit as he could before close, reverse-swept Smith for consecutive boundaries before launching into a heave over midwicket for a six to bring up the century partnership. Faisal Iqbal, who started off quietly, got into the act as well with back-foot drives through midwicket as well as a few streaky strokes past slips. He fell to Paul Harris with only four overs of the day remaining but his 114-run partnership with Younis gives Pakistan a ray of hope.
It seemed impossible that Pakistan might even be contemplating victory when South Africa declared their innings after Kallis scored his 26th Test century and became only the fourth South African to score hundreds in each innings of a Test, and the first since Gary Kirsten.
Kallis' second-innings effort, a stubborn and flawless unbeaten 100, was in sharp contrast to his 155 in the first innings and took 201 balls. Mohammad Asif did not feature in the attack at all on the day and Umar Gul and Abdur Rehman opened the bowling for Pakistan. Kallis drove through cover and cut past backward point early on before settling down into taking singles and twos.
Although South Africa lost Ashwell Prince early - playing a Danish Kaneria delivery onto his stumps - and AB de Villiers to a peach of a turner from Rehman, Kallis ensured a constant flow of runs. He was particularly severe on Kaneria, dispatching the short offerings to either side of the wicket. Kallis drove elegantly through extra cover to bring up his fifty just before lunch.
Rehman forced another breakthrough against the run of play as he tempted Mark Boucher, who stitched together a 88-run partnership with Kallis, into playing a slog sweep straight down the throat of long-on before Andre Nel provided a few fireworks while reaching his highest score in Test cricket.
Kallis had perhaps been asked to reach the milestone before tea, and launched into a massive heave over the long-off boundary - his first boundary in 55 balls - before taking four singles to reach his ton. On cue, Smith motioned to his batsmen from the dressing room.
Pakistan had yet another poor performance with the ball and in the field. Kaneria did manage to get a lot of turn but he strayed on both sides of the wicket and was either left alone or padded away. Gul, after bowling only three overs in the morning, returned with pace in his second spell but lacked penetration. Hafeez was used sparingly and rightly so as his innocuous turners only added to the scoring opportunities. Rehman, bowling 24 overs in the day, capped off a successful debut and his dismissal of de Villiers - ball pitching outside leg stump and turning viciously to hit off - will only strengthen his case to be a regular member of future teams.