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. (Agencies)
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.
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.