Du Plessis and Duminy put on their century stand after opener Henry Davids had been dismissed off the first ball of the match, leg-before to seamer Nuwan Kulasekara.
Duminy hit his 100th six in Twenty20 internationals when he lofted Kulasekara over the mid-wicket fence in the 17th over.
Du Plessis was dismissed in the final over, bowled by Lakmal. The hosts, already down 0-2 in the series, launched a spirited chase of South Africa's challenging 163-3 to win the day-night game in Hambantota with 11 balls to spare.
South Africa won the series 2-1, making amends for the 1-4 defeat in the preceding one-dayers on the tour.
But Tuesday's victory was crucial for Sri Lanka, who would have slipped to number three in the T20 rankings behind Pakistan and South Africa had they been whitewashed 3-0.
Dilshan brought up the thrilling win off the first ball of the 19th over, lofting Morne Morkel for his second six.
He also hit nine boundaries.
Former captain Mahela Jayawardene hit a quick