South Africa 343 for 5 (Amla 122, de Villiers 128) beat Pakistan 309 (Afridi 88, Hafeez 57) by 34 runs
Scorecard and ball-by-ball details

South Africa were pretty and effective in pink as they took the series lead in a thriller at the Wanderers. But they almost saw red as Shahid Afridi scored his first half-century since February last year to keep Pakistan in the game well into its latter stages.

Hashim Amla and AB de Villiers shared a record third-wicket stand of 238 and scored at 7.84 to the over to set Pakistan a massive target before South Africa's bowlers used variation to stub out any chance of a successful chase.

After taking a bruising the field, with the third seamer Wahab Riaz conceding 93 runs, the most number of runs by a Pakistan bowler in an ODI, they needed someone to bat through. Mohammed Hafeez appeared the man up for the task with an aggressive half-century, but middle order wobbles meant it was up to Afridi to bludgeon the team home and he almost did.

To begin with, at 26 for 2, having faced 48 dot balls in 10 overs, it did not look as though South Africa would post a 300-plus score. It took 14 balls before they recorded their first run and both Graeme Smith and Colin Ingram grew impatient with the difficulties of contending with extra bounce from Mohammad Irfan and movement into the right-hander from Junaid Khan.

Hashim Amla was given out lbw off Junaid's second ball but it was found to have pitched just outside leg on review. That was one of the lifelines given to Amla, the other came when he was dropped on 36, edging to point after a somewhat shaky start.

Amla had de Villiers' boundless energy to keep the run-rate moving while he settled in and when he did, Pakistan were left to rue their generosity. De Villiers showed his intent as he arrived at the crease when he broke a boundary drought that had lasted 36 balls, and he did not stop there.

Misbah-ul-Haq saved Saeed Ajmal until the half-way point in the innings, and de Villiers and Amla tucked in. They brought out a range of shots as eyecatching as the pink kit they were wearing and a reminder of why they are ranked No. 1 and No. 2 in the world. They had it all: sweeps, reverse-sweeps, lofts, pushes, glances and cuts, all expertly timed and sprinkled with delicate touches.

De Villiers twirled his wrists to beat point and third man and moved across his off-stump to ramp Wahab over short-fine. He fell in the process but never lost control of the shot. His back-to-back sixes off Hafeez, a slog sweep and then a smash over extra cover were arguably the best of his innovative knock. His century came off 87 deliveries.

Amla's was also scored at better than a run a ball, his taking 99 balls. Placement was his hallmark as he peppered the covers with beautiful drives. He mixed that with aggression, evident in his smack off Afridi over long-off. His footwork, especially his dance across the offstump to hit on the on-side, remained unmatched for its quality.

As the stand grew and the pair threw caution to the wind, they both fell. Amla was caught at extra cover and de Villiers top-edged to midwicket after bringing up the 300. Their dismissals would have done nothing to lift Pakistan, though. Faf du Plessis promptly recorded the most expensive over of the innings when he dispatched Wahab for 20 runs and his cameo ensured Pakistan would have a big battle on their hands.

They stuttered from the get-go when Nasir Jamshed gave away his wicket, chipping a good length, slower ball to mid-off.

Kamran Akmal was promoted up the order again, a move that brought some success. He pouched on width from Lonwabo Tsotsobe and Steyn's short ball. There was still a degree of circumspection to his and Hafeez's approach but they scored fairly quickly for the 13 overs they were together.

They accumulated 82 runs and found the boundary 13 times, Hafeez hitting back to back sixes off Robin Peterson to bring up his half-century. Hafeez and Kamran took Pakistan to three runs short of a 100 inside 17 overs before Akmal tried to upper cut a Ryan McLaren slower bouncer but edged behind.

Hafeez retreated into a shell once Younis Khan arrived and eventually the frustration boiled over. He holed out to mid-on off Robin Peterson, where Steyn, who has dismissed him five times all told on the tour, was waiting. South African celebrations were wild as they thought they had removed the biggest threat.

But there was another to come. Afridi showed some of his boom boom in the first ODI in Bloemfontein but brought the rest to Johannesburg. None of the seamers executed the yorker Allan Donald promised they had been practicing and offered Afridi length, which he lapped up. He cleared the boundary, over mid-wicket and long-off, four times to get South African nerves jangling as he brought up a half-century off 31 balls.

Misbah's dismissal did not stop him, if anything, it only spurred him on. Despite carrying a bruised thumb after being hit by Steyn, he swatted four more boundaries before inside-edging onto his stumps off a McLaren low full toss on 73. Replays showed it was a no-ball.

Afridi made a mockery of McLaren's free-hit delivery, smoking him for what could be the biggest six seen at the Wanderers. The ball landed on the roof of the stand at the Golf Course End and then rolled over it.

In the next over, Afridi was dropped by Smith at deep extra cover but his luck eventually ran out. Tsotsobe still missed the yorker length and offered a full toss, which Afridi hit to long-off where McLaren took the catch. He checked for a no-ball because of the height but the delivery was legitimate and an entertaining knock ended with Pakistan still needing 100 runs to win.

It was expected to be a bridge too far but Wahab came close to crossing it. He batted for seven overs with Saeed Ajmal, putting on 38 runs to set up a final assault. When Ajmal was caught at mid-off, Wahab kept going, placing his shots well and finding gaps.

Junaid Khan also abandoned him and it was up to Irfan, who had earlier left the field with an upset tummy, to hang on until the end. But it was Wahab who could not. He was yorked by Kleinveldt, who finally got it right, at the start of the 49th over to end a spirited contest.

Firdose Moonda is ESPNcricinfo's South Africa correspondent