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