Pakistan were inspired by a fiery unbeaten 93 from Younis Khan as they chased a record South African target featuring Jacques Kallis's second successive century in the first test on Thursday.
Pakistan, facing a world record chase of 424 runs, finished the fourth day on 146-3 with nightwatchman Mohammad Asif on one, needing another 278 runs to win the Test in the remaining 90 overs on Friday.
Just when Younis and Faisal Iqbal (44) threatened the tourists with a solid 114-run stand for the third wicket, left-arm spinner Paul Harris dismissed Iqbal with three overs to go to set up an intriguing last day.
South Africa were indebted to Kallis who became only the fourth Proteas batsman to score a century in each innings of a Test. Kallis remained unbeaten on 100 to help his team declare their second innings at 264-7.
The world record for the highest target achieved was 418 which the West Indies made against Australia in Antigua in May 2003. Pakistan's highest successful chase is 315-9 against Australia made at the same ground in 1994.
Pakistan had a disastrous start to their second innings, losing Mohammad Hafeez (one) in the second over of the innings when he played on to a Dale Steyn delivery.
Steyn then removed Salman Butt (three) in his third over when close-in fielder Hashim Amla smartly took a bat-pad push to leave Pakistan struggling at 20-2.
Younis, who managed just six in Pakistan's first innings score of 291, made amends as he raced to his 50 off just 51 balls with his eighth boundary. Younis has so far hit 14 boundaries and a six during his 99-ball knock.
Pakistan now need a big effort from their batsmen to win or save the Test at the venue where they have only lost to England (2000) in 39 previous Tests.
It was Kallis who put the tourists on the winning track as his second successive high-class knock demolished the home team's hopes of a fightback.
"To score two separate hundreds in a Test is an achievement I am proud of," said Kallis. "It's a great feeling and I am very happy."
Kallis hoped Pakistan's attempt to go for runs will also help the tourists.
"It will be an interesting final day. If Pakistan go for a win then we will also get wickets. We did not bowl well in the second innings and Younis surprised us with his attacking batting."
Kallis hit only four boundaries and a six during his nearly five-hour stay at the crease, proving his claim to be the best South African batsmen in the longer version of the game.
The 31-year-old added 88 runs for the fourth wicket with Ashwell Prince (45) and another 63 runs for the sixth wicket with Andre Nel who made a Test best of 33.
Nel's previous best of 23 not out was also against Pakistan at Port Elizabeth earlier this year.
Kallis, subdued after lunch with no boundaries for 55 balls, lifted Rehman to go within four runs of a twin hundred in the match. He took them all in singles to reach three-figure mark off 210 balls in 289-minutes.
Alan Melville, Bruce Mitchell and Garry Kirsten were the other South Africans to score a century in each innings in Tests.
The tourists, looking for a match-winning margin on a spin-friendly pitch, resumed on 76-3 with a steady Kallis and Prince. The duo took the score to 132 without facing any problems.
Kaneria, surprisingly kept out of attack in the first hour, provided the home team with the much-needed breakthrough.
Rehman then baffled AB de Villiers with a sharp turner to bowl the batsman for one.
Debutant Rehman, who took four wickets in the first innings, was the pick of home bowlers with 4-105. Kaneria finished with 3-85.
The second and final Test starts in Lahore from October 8.