Pakistan's Sarfraz Ahmed and Mohammad Amir put on a record eighth-wicket stand of 75 to take their team through to the semi-finals of the ICC Champions Trophy at the expense of Sri Lanka.
Sarfraz was dropped twice in the closing stages from the bowling of Lasith Malinga, but rode his luck to finish unbeaten on 61 from 79 balls, having found a reliable partner in Amir (28no from 43) as the Pakistanis reached their target of 237 with 31 balls and three wickets to spare.
Sri Lanka had looked almost certain to progress when Pakistan's middle-order fell to 7-162 following a blazing start from Fakhar Zaman (50 from 36), with the middle-order trio of Babar Azam (10), Mohammad Hafeez (1) and Shoaib Malik (11) all failing to make an impact on proceedings.
And when Imad Wasim was out for four and debutant Fahim Ashraf (15) was run-out after being slow to react at the non-striker's end, Pakistan looked like producing one of their famous implosions.
But Sarfraz and Amir's rescue act turned the tide in a thrilling contest, the pair picking singles amid some hazardous running and finding the rope at crucial intervals to see their team home.
Pakistan began the run chase favourites after earlier restricting Sri Lanka to 236 all out.
Opening batsman Niroshan Dickwella made a fine 73 as Sri Lanka's innings showed early promise, but the Pakistanis came roaring back into the contest thanks to some fine bowling from Hasan Ali (3-42), Junaid Khan (3-40) and Mohammad Amir (2-53).
Sarfraz won the toss and Junaid got the early breakthrough, removing Danushka Gunathilaka for 13, before Kusal Mendis (27) and Dinesh Chandimal (0) fell in quick succession.
Dickwella, who had earlier signalled his attacking intentions with a stunning scoop shot for four, teamed up with captain Angelo Mathews (39) and for a while it looked as if the Sri Lankans were on track for a sizeable total.
At 3-161 in the 32nd over, Amir knocked over Mathews to claim his first wicket of the tournament and trigger a dramatic collapse of four wickets for just six runs.
Amir took a second while Junaid took two more as the pace pair followed Hasan's lead, extracting movement from the pitch and through the air in an impressive display of fast bowling.
Asela Gunaratne (27) and Suranga Lakmal (26) put on 46 for the eighth wicket to help Sri Lanka post what was in the end a moderate total, but the island nation will need an outstanding bowling effort if they're to progress through to the semi-finals.
Sri Lanka batsman Dhananjaya de Silva was included after batsmen Chamara Kapugedera and Kusal Perera were both ruled out for the remainder of the tournament, while teenage leg-spinner Shadab Khan was left out of the Pakistan XI, with debutant quick Fahim Ashraf (2-37) included in his place.
Champions Trophy 2017 Guide
Squads: Every Champions Trophy nation
2 June – New Zealand v Australia, No Result
4 June – India beat Pakistan by 124 runs
5 June – Australia v Bangladesh, No Result
6 June – England beat New Zealand by 87 runs
11 June – India beat South Africa by eight wickets
12 June – Pakistan beat Sri Lanka by three wickets
14 June – First semi-final (England v Pakistan), Cardiff (D)
15 June – Second semi-final (Bangladesh v India), Edgbaston (D)
18 June – Final, The Oval (D)
19 June – Reserve day (D)