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.
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.
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.
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.
