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.
Whe