Pakistan survived a batting collapse as they capitalised on an error-strewn Sri Lanka performance to progress to the ICC Champions Trophy semi-finals with a three-wicket win in the game of the tournament.
Despite Niroshan Dickwella's 73, the Pakistan attack - led by Junaid Khan (3-40) and Hasan Ali (3-43) - forced a Sri Lanka collapse from 161-4 to 236 all out at Sophia Gardens.
Fakhar Zaman's rapid half-century fired Pakistan to 74-0 in reply, but they slumped to 162-7, setting up a fascinating and thrilling conclusion.
Captain Sarfraz Ahmed (61 not out) held the key to the Pakistan innings and Sri Lanka, with victory in sight, twice dropped the skipper as he posted a crucial half-century.
Sri Lanka's bemusing errors in the field aided Sarfraz and Mohammad Amir (28 not out) in their match-winning stand of 75, the captain ramping a four to the boundary at third man to book a semi-final with England that had looked extremely unlikely after their opening 124-run defeat to India.
Dickwella's solid start gave little hints of the low-scoring thriller that was to come, but the seeds were sown with Kusal Mendis (27) and Dinesh Chandimal (0) falling inside three deliveries as Fahim Ashraf (2-37) claimed his maiden ODI wicket.
Captain Angelo Mathews (39) helped steady the ship but his wicket - Amir's (2-53) first of the tournament - prompted a dismal slump.
A ripper from Junaid did for Dhananjaya de Silva (1) before a stunning low catch from Sarfraz ended Dickwella's admirable effort and Thisara Perera (1) lasted just five balls.
Suranga Lakmal (26 off 34) provided a useful contribution only for a Hasan bail trimmer to halt his progress. Asela Gunaratne was expansive in his 44-ball 27 but fell when he hit Hasan to deep midwicket.
Disappointing loss. At the end forget @OfficialSLC batting or bowling the fielding let the team down badly. It was there to be won.-- Kumar Sangakkara (@KumarSanga2) June 12, 2017
Azhar Ali survived a drop and a run-out chance in the opening two overs, but he and Fakhar scored freely, the latter reaching 50 in 34 balls before falling to Nuwan Pradeep (3-60), who claimed his second scalp when Babar Azam (10) picked out midwicket.
Pradeep took the catch as Mohammad Hafeez (1) fell cheaply, and Sri Lanka's confidence was further boosted when Azhar (34) edged Lakmal (1-48) to first slip.
Shoaib Malik (11) and Imad Wasim (4) both failed to make a significant impression, and Pakistan's luck appeared to be out when Perera ran out Fahim (15) by deflecting a Sarfraz drive onto the stumps.
But fortune turned in Pakistan's favour when Perera dropped a dolly from Sarfraz on 38 and substitute fielder Seekkuge Prasanna was unable to take a stooping chance at square leg.
The errors mounted as Sri Lanka's composure completely disappeared, a drive through mid-off bringing up Sarfraz's 50 before he fittingly completed the recovery mission with 31 balls remaining.