Afridi scored an unbeaten 20-ball 39, hitting the winning six off the last ball of the final over by paceman Nuwan Kulasekara to give Pakistan a 1-0 lead in the two-match series.
In all Afridi hit three sixes and two boundaries to help Pakistan overcome a tough challenge from world number one Sri Lanka who had taken the match to the final over with Pakistan needing six runs.
Set a challenging 146 to get, Pakistan lost Ahmed Shehzad (four) in the third over but Mohammad Hafeez (32) and Sharjeel Khan (34) steadied the innings through their 57-run second wicket stand before they lost three wickets in the space of seven runs.
Sharjeel holed out after hitting three fours off 31 balls while Hafeez, who hit two fours and a six in his 27-ball knock, fell leg-before and Umar Akmal was run out for five.
It was left to Afridi to see Pakistan through. Criticised for his recent poor batting form, Afridi hit two towering sixes in Kulasekara's 16th over to give Pakistan a sniff of victory.
When on 23, Afridi also became the first player to score 1,000 runs and take 50 wickets in all Twenty20 cricket. In all he has 73 wickets in 69 matches.
Afridi said he was delighted to return to his aggressive batting. "Pakistan won and I'm very happy for that. I have to mention (skipper) Hafeez, who was not feeling well but still he played for the country.
"It's nice to bat well, once in a blue moon."
Sent into bat, Sri Lankan batsmen did not capitalise on the good starts before Mathews (50) and Lahiru Thirimanne (23 not out) added 58 for the fifth wicket stand to take their team to 145-5 in 20 overs.
Mathews hit five boundaries and a six off 34 balls before holing out in the last over. He hit paceman Bilawal Bhatti for three boundaries in one over before sending left-arm paceman Sohail Tanvir to long-on ropes to reach his fifty off 32 balls.
Experienced opener Tillakaratne Dilshan managed just seven before he was the first wicket to fall in the second over.
Kusal Perera (15) and skipper Dinesh Chandimal (22) then added 32 for the second wicket but off-spinner Saeed Ajmal put on the initial brakes with 2-35.
Kumar Sangakkara scored 21.
Pakistan gave an international debut to 19-year-old Usman Shinwari after the left-arm paceman was drafted into the team as late as Tuesday but the youngster from the troubled Kahyber Pakhtunkhwa province bowled only one over, costing nine runs.
Tanvir finished with 2-32 while leg-spinner Shahid Afridi took 1-20 in his four overs. The second match will also be played in Dubai on Friday. The two teams also play five one-day internationals and three Tests.
Photos: Salman Khan returns from Jodhpur to Mumbai
15 'peach' perfect photos of Priyanka Chopra at People's Choice Awards 2017
Photos: Tennis legend Andre Agassi meets Rahul Dravid in Mumbai
62 going on 16: These photos of Rekha prove she is 'forever young'
Photos: 10 times 'Sultan' Salman Khan wrestled with the law