Hashim Amla (98) and AB de Villiers (74) took South Africa to the brink of victory but they could score only seven runs off the last nine balls, while losing two wickets, to finish on 261 for six
Needing 263 to win in a game reduced to 45 overs a side by morning rain, South Africa had fallen well behind the required run rate when De Villiers joined Amla.
De Villiers slammed 74 off 45 balls as he and Amla put on 110 off 78 deliveries to reduce the chase to less than a run a ball.
But De Villiers played one audacious shot too many to be caught at deep backward square leg. Amla and JP Duminy got South Africa to a stage where they needed nine runs off nine balls. But Saeed Ajmal finished a superb last over by having Amla caught in the deep.
Then Duminy fell to Junaid Khan in the last over. Eight were needed off the last four balls and Junaid allowed only two singles and four leg byes. The Pakistan players ran to embrace each other, with some kissing the ground.
Pakistan scored 262 all out. Opening batsman Ahmed Shehzad made 102 after Pakistan were sent in to bat under heavily overcast skies.
Fast bowler Dale Steyn took a career-best six for 39, including two wickets in the last over of the innings, but the other South African bowlers struggled to contain their opponents, who won the first match in Cape Town by 23 runs on Sunday.
Shehzad, 22, hit eight fours and two sixes in a 112-ball innings which ended unhappily when he hit the ball to mid-on and set off for a run before being sent back by batting partner Umar Akmal. He could not beat Imran Tahir's throw to wicketkeeper Quinton de Kock and gestured angrily at Akmal.
The dismissal came off the last ball before the compulsory batting power play, which was taken after 35 overs. Shehzad, who made his first-class debut aged 15, shared a third wicket stand of 124 off 134 balls with Sohaib Maqsood, who made 42 off 59 balls.
The pair came together after Steyn had snared two early wickets, bowling Nasir Jamshed and extending his ascendancy over Mohammad Hafeez, who he had caught behind. It was the 15th time Steyn had dismissed Hafeez in international matches.
South Africa struggled to build any momentum at the start of their innings and had scored only nine when Graeme Smith was caught behind off Junaid Khan.
Amla and Quinton de Kock (47) put on 87 off 103 balls for the second wicket. Although Jacques Kallis was out cheaply, a platform had been established for De Villiers to play a memorable innings. But it was not quite enough.
The last game of the three-match series is in Centurion on Saturday.