Dilshan was unbeaten on 115 and Sangakkara made 91 during a record second-wicket partnership of 184 runs to take the hosts past South Africa's 238.
Dilshan's 17th ODI century was studded with 16 boundaries. Sangakkara kept pace with his partner with eight fours and two sixes. The pair put on Sri Lanka's highest ODI stand for any wicket against South Africa. Sangakkara missed his second hundred in the series when he holed out in the deep off Morne Morkel when just ten more were needed for victory.
Earlier, JP Duminy hit 97 and Hashim Amla made 77 to lift South Africa to 238 after they won the toss and elected to bat. But the South Africans undid a good start by losing eight wickets for 87 runs after cruising at 118 for 1 in the 22nd over.
Duminy lashed seven boundaries and two sixes before he was last man out in the 49th over, bowled by Ajantha Mendis three runs short of a century. Mendis finished with 4 for 51, while Lasith Malinga claimed 3 for 52 and Dilshan took two wickets.
Amla, batting for the first time in the series after being injured while fielding in the only other game he played, anchored the start with a fluent half-century that contained nine boundaries. Duminy shared a second-wicket stand of 101 with Amla after Malinga had bowled Quinton de Kock in the third over.
Dilshan removed Amla lbw and had AB de Villiers caught behind by Sangakkara in the space of eight runs to make it 126 for 3 in the 25th over.