Pakistan will meet Group A toppers England in the first semifinal on Wednesday at the same venue.
Chasing a modest 237 to win, opener Fakhar Zaman (50) cracked a 36-ball 50 at the top but Pakistan suffered an inexplicable collapse to slip to 162-7.
Finally, it was Sarfraz Ahmed, who played a resolute captain's knock to take Pakistan home. The wicket-keeper batsman shared 75 runs with Mohammad Amir (28) for the 8th wicket as Pakistan overhauled the target with 31 balls to spare.
Earlier, Fast bowlers Hasan Ali (3/43) and Junaid Khan (3/40) claimed three wickets each, while Amir (2/53) and Fahim Ashraf (2/37) accounted for two wickets to help Pakistan bowl out Sri Lanka for 236 after electing to field.
More From This Section
Opener Niroshan Dickwella (73) and skipper Angelo Mathews (39) added 78 runs for the fourth wicket but Amir and Junaid took two wickets each in the space of 3.2 overs as Sri Lanka lost four wickets for six runs to slip to 167-7.
Chasing a modest 237 to win, Fakhar gave Pakistan a flying start as he alongwith Azhar Ali shared a 74-run opening partnership in 11.1 overs.
Fakhar blasted eight boundaries and one six to score his fifty but he top-edged a Nuwan Pradeep delivery next to Gunaratne at fine leg as Pakistan lost their first wicket in the 12th over.
Pakistan then lost three quick wickets -- Babar Azam (10), Mohammad Hafeez (1) and Azhar Ali (34) to slip from 92-1 to 110-4 in the 20th over.
Skipper Sarfraz Ahmed, who was dropped twice in the 39th and 41st over off Malinga, made good use of the chances to produce a match-winning innings and in company of Amir took the team home.
Earlier, Danuhska Gunathilaka (13) was scalped early by Junaid but Dickwella and Kusal Mendis (27) stitched a 56- run stand to keep Sri Lanka going.
Things looked settled for the Island nation before Hasan Ali came up with a fiery spell, in which he troubled Mendis with pace and bounce before eventually bowling him out.
Two wickets in three balls, however, did not bother Sri Lanka much as Dickwella remained solid at the other end and soon completed his half-century with a single off Mohammed Hafeez.
Skipper Mathews supported his colleague with a patient knock. The two batsmen rotated the strike quite easily, which not only kept the scoreboard ticking, but also put pressure back on Pakistan.
Frustration grew among Pakistan players with Mathews and Dickwella raising a resolute 78-run stand for the fourth wicket in 16.1 overs.
It was the first wicket of the tournament for Amir and it came at a crucial time for Pakistan. Junaid sent back new man Dhananjaya de Silva (1) and Amir returned to dismiss Dickwella, who fell to a sensational low catch by wicketkeeper Sarfraz Ahmed.
Junaid saw the back of Thisara Perera to rattle the Lankan innings.