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 quickfire 33 in an opening stand of 67 off 35 balls with Dilshan to help Sri Lanka make a rollicking start.
The hosts were 123-4 in the 16th over when Thisara Perera (25 not out) put on a match-winning stand of 41 with Dilshan for the fifth wicket.
With 27 runs needed off the final 18 balls, the big-hitting Perera took 21 runs from the 18th over bowled by Wayne Parnell, including three fours and a six, to ease the pressure.
"It was not an easy target to chase, but the start I got with Mahela helped," said Dilshan, who was named man of the match.
"After that, I knew we could pass that total. It was important not to panic, but Thisara made the job easier. We may have lost the series, but it feels good to win this game."
South African captain Faf Du Plessis said the series win will boost his team ahead of next year's World Twenty20 in Bangladesh. "It's a big trophy to take back on the plane," he said.
"We are really happy to win the series against the best team in the world. I thought 163 was enough, but one has to give credit to Dilshan. "This series was a good experience for the boys because the conditions in Bangladesh will not be much different from here."
The hosts rested their two main players, batsman Kumar Sangakkara and fast bowler Lasith Malinga, and recalled Dilshan and Suranga Lakmal.
Earlier, du Plessis hit 85 off 65 balls and JP Duminy remained unbeaten on a 34-ball 51 as South Africa too put on a thrilling exhibition of power hitting.
Du Plessis, who won the toss for the third time in a row and elected to bat, smashed 10 boundaries and three sixes during a third-wicket stand of 112 with JP Duminy.
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.
Spotted: Twinkle Khanna, Shriya Saran at an event in Mumbai
Andrew Flintoff's birthday: 10 players who entered different sports
Photos: Ranveer Singh and Vaani Kapoor go 'Befikre'
Photos: Aishwarya, Sridevi, Karisma at Manish Malhotra's birthday bash
Photos: Salman Khan, Shah Rukh Khan, other celebs at Star Screen Awards