Miller plundered a robust 85 not out off 72 balls to steer the team to 223 for 7 after Ajantha Mendis had reduced South Africa to 154 for 7 in the 42nd over. Tsotsobe then ripped through the Sri Lankan top order with three wickets in the space of six runs before finishing with 4 for 22, as Sri Lanka was 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' side 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 an otherwise gloomy evening for Sri Lanka by smashing five sixes and a boundary in the 33rd over sent down by Robin Peterson, the left-arm spinner.
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 Herschelle Gibbs, the former South Africa batsman, off Daan van Bunge, the Netherlands bowler, 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 Sri Lanka to bring home an unlikely win. 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 3 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 for 5 after South Africa had won the toss and elected to take first strike.
De Villiers led the recovery with 47, before Miller pushed the scoring with his fifth one-day half-century and 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.