David Miller and Lonwabo Tsotsobe played key roles as South Africa stunned hosts Sri Lanka by 56 runs in the third one-dayer in Pallekele on Friday to stay afloat in the five-match series.
Miller plundered a robust 85 not out off 72 balls to steer South Africa to 223-7 after unorthodox spinner Ajantha Mendis had reduced the tourists to 154-7 in the 42nd over.
Tsotsobe then ripped through Sri Lanka's top order with three wickets in the space of six runs before finishing with 4-22 as the hosts were bundled out for 167 in the day-night game.
The victory ended South Africa's 11-match losing streak in one-day cricket on Sri Lankan soil and gave AB de Villiers' tourists a badly-needed win in the current series.
Petersen c Sangakkara b Malinga 8
Q de Kock c Sangakkara b Perera 20
JP Duminy b Perera 23
AB de Villiers c Sangakkara b Mendis 47
F du Plessis run out 16
F Behardien b Mendis 2
DMiller not out 85
R Peterson b Mendis 3
R McLaren not out 14
TOTAL: (for seven wickets) 223
Fall of wickets: 1-14, 2-50, 3-53, 4-95, 5-100, 6-148, 7-154.
U Tharanga c Petersen b Tsotsobe 5
T Dilshan c de Villiers b Tsotsobe 6
K Sangakkara c Duminy b Tsotsobe 0
M Jayawardene b Peterson 24
D Chandimal c de Kock b McLaren 29
A Mathews c de Villers b Tsotsobe 14
A Perera c de Villiers b Berhardien 1
T Perera c du Plessis b Berhardien 65
R Herath not out 12
L Malinga b Berhardien 0
A Mendis c Tsotsobe b Duminy 0
TOTAL: (all out) 167
Fall of wickets: 1-14, 2-14, 3-16, 4-71, 5-78, 6-81, 7-93, 8-165, 9-166.
With Sri Lanka 2-1 ahead, South Africa must also win the fourth match at the same venue on Sunday to take the series into the decider in Colombo next Wednesday.
Thisara Perera lit up the gloomy evening for the hosts by smashing five sixes and a boundary in the 33rd over sent down by left-arm spinner Robin Peterson.
With the second ball declared a wide, the 35 runs taken in the over fell just one run short of the world record of six sixes hit by South African batsman Herschelle Gibbs off Dutch bowler Daan van Bunge during the 2007 World Cup.
Perera's run sequence in the over was six, a wide, six, six, six, four and a six, but his sensational hitting came too late for the hosts to deliver an unlikely win.
"It's hard to explain how proud I am of the boys," de Villiers said. "It was not easy and we were under pressure through the game.
"It was a really good performance. But we did not have enough partnerships going."
Left-handed Perera made 65 off 49 balls with five sixes and as many boundaries, before he was caught in the deep off Farhaan Behardien, who finished with three for 19.
Sri Lanka paid the penalty for a shoddy batting display in which six batsmen failed to reach double figures and only two others entered their 20s.
Sri Lankan captain Angelo Mathews, returning to the side after serving a two-match suspension due to his team's slow over-rates in the tri-series in the West Indies, paid rich tributes to Perera.
"He is turning out to be a good all-rounder," said Mathews. "The pitch was not easy to bat on, but it looked as if Thisara was batting on a different wicket.
"With Thisara playing so well, I should probably have stayed there with him. We need to get everything together on Sunday."
Earlier, Miller hit five sixes and four boundaries to boost the total from 100-5 after South Africa had won the toss and elected to take first strike.
Skipper de Villiers led the recovery with 47, before left-handed Miller pushed the scoring with his fifth one-day half-century, the first by a South African in the series.
Miller put on 69 for the eighth wicket with Ryan McLaren, who remained unbeaten on 14.