Pakistan recovered from 40-5 in the eighth over to surpass Sri Lanka's challenging 172-7 with four balls to spare with Imad Wasim striking the winning six off left-arm seamer Binura Fernando.
Skipper Afridi led the way with a 22-ball 45 that included four sixes and a boundary, before Anwar smashed 46 off 17 balls in a match-winning knock that contained four sixes and three fours.
Imad, who remained unbeaten on 24, helped Anwar add 58 for the eighth wicket as the tourists recorded a heart-stopping win in front of a sell-out crowd of 35,000 at the Premadasa stadium that briefly included Sri Lanka President Maithripala Sirisena.
Fernando, a lean 20-year-old playing only his second international match, gave Sri Lanka a flying start with a double strike in his second over.
He bowled Ahmed Shehzad with a slower delivery and then forced Mukhtar Ahmed to top-edge a bouncer to Chamara Kapugedera at mid-wicket.
Pakistan's top order caved in as Mohammad Hafeez and Umar Akmal were run out and senior batsman Shoaib Malik was stumped.
Afridi began the amazing turnaround with a sixth-wicket stand of 61 with Mohammad Rizwan (17) as the inexperienced Sri Lankan attaack wilted under pressure.
Jayasuriya, a 23-year-old left-hander unrelated to Sri Lanka's former captain Sanath Jayasuriya, smashed three fours and two sixes in his 32-ball 40 before he was fifth out.
But it was Kapugedera who boosted the total with an unbeaten 48 off 25 balls that contained four sixes and two boundaries.
Kapugedera put on 55 for the sixth wicket with Milinda Siriwardana (23) as the hosts plundered 59 runs in the final five overs.
