Pakistan booked a Champions Trophy semi-final clash against England on Wednesday with a three-wicket victory over Sri Lanka.
Sri Lanka: 236 (49.2/50 overs, Dickwella 73, Mathews 39; Junaid Khan 3-40, Hasan Ali 3-43)
Pakistan: 237-7 (44.5/50 overs, Ahmed 61, Zaman 50; Fernando 3-60)
When they slumped from 92 for one to 137 for six in pursuit of 237 for victory, Pakistan looked down and out at Cardiff Wales Stadium.
But a return to the Welsh capital in under 48 hours' time was secured thanks to the efforts of captain Sarfraz Ahmed and opening bowler Mohammad Amir.
They compiled an unbroken eighth-wicket partnership of 75, with Sarfraz hitting an unbeaten 61 and Amir undefeated on 28, to leave Sri Lanka crestfallen and reflecting ruefully on dropping Sarfraz twice in quick succession as the Pool B contest reached a thrilling conclusion.
Sri Lanka, despite a battling 73 by opener Niroshan Dickwella, were bowled out for what appeared an under-par total, with Junaid Khan and Hasan Ali each taking three wickets as headline acts of Pakistan's pinpoint seam attack.
Requiring under five runs an over, Pakistan set about their task in clinical fashion as Fakhar Zaman and Azhar Ali delivered a 74-run opening partnership.
But such promise rapidly evaporated as Pakistan lost wickets cheaply, not helping themselves through some poor shot choices.
Sri Lanka did not require a second invitation to go for the jugular, giving themselves sight of an unlikely win before it was taken away from them by an inspired captain's innings, securing the win with 5.1 over to spare.
After being put in to bat, Sri Lanka's openers made a confident start, initially easing along at almost six runs an over during a spell highlighted by Dickwella walking across his stumps and flicking an audacious scoop shot off Junaid Khan way over Pakistan wicketkeeper Sarfraz's head which almost carried for six.
But Pakistan broke through in the sixth over when Danushka Gunathilaka tamely chipped Khan to mid-off, where Shoaib Malik took an easy catch as the opener departed for 13 and left his team 26 for one.
Pakistan's hopes of making further early in-roads were dashed, though, as Dickwella and Kusal Mendis impressively went about their work, mixing raw attacking aggression with subtle shots on both sides of the wicket as they compiled a confident second-wicket partnership.
Pakistan needed a breakthrough and it arrived right on cue when Hasan Ali clean-bowled Mendis with a brilliant delivery that moved late to leave Sri Lanka on 82 for two.
Fahim Ashraf then marked his one-day international debut by dismissing Dinesh Chandimal for a second-ball duck when he dragged an attempted cover drive on to the stumps.
It left Sri Lanka with a rebuilding job entrusted to Dickwella and captain Angelo Mathews and they did it impressively, adding 78 for the fourth wicket in relatively trouble-free fashion.
But then the innings began to crumple.
Mathews became Amir's first wicket of the tournament when he played on for 39 and then, with only one more run added, Dhananjaya de Silva edged the ball to Sarfraz and was out for one, before Sri Lanka's rock Dickwella fell victim to a stunning catch by the Pakistan wicketkeeper.
Amir cajoled a thin inside edge from Dickwella, which meant Sarfraz changing direction before catching the ball low down with his left hand, and Dickwella's resilient innings ended on 73.
Khan claimed his third wicket when Thisara Perera's thick edge was snapped up by first slip Babar Azam.
Sri Lanka had lost four wickets for six runs in 25 balls, leaving them with a Herculean task to post a competitive score and keep alive any realistic semi-final hopes.
Asela Gunaratne and Suranga Lakmal took them past 200 with a 46-run partnership highlighted by Lakmal's 34-ball 26.
Gunaratne holed out, caught just inside the mid-wicket boundary by Zaman off Hasan Ali, who had bowled Lakmal, for 27, before Pradeep gave Ashraf an easy caught and bowled as Sri Lanka's innings ended two balls into the 50th over.
Any early hope of them pinning Pakistan's reply on to the back foot were threatened by a 34-ball half-century from Zaman, but things began to change when Pakistan lost him, Mohammad Hafeez and Azhar Ali in quick succession, while adding just 18 runs.
And when Malik was caught behind for 11, then Imad Wasim departed to leave Pakistan 131 for six, Sri Lanka looked to have prised open the door to an unlikely victory, which gathered momentum as Ashraf was run out at the non-striker's end for 15, leaving his team requiring another 75 runs with just three wickets left.
But Ahmed and Amir came up trumps when it mattered, leaving Sri Lanka to reflect what might have been, particularly Perera, who dropped a sitter at mid-on off Sarfraz when Pakistan were seven wickets down and still 43 runs short.
Pakistan captain Sarfraz Ahmed is wary of the threat posed by England ahead of Wednesday's Champions Trophy semi-final in Cardiff.
"I think it's a great win today, and credit goes to the bowlers, who bowled really well at a crucial time," Sarfraz said.
"Yes, there is a little bit of concern about the middle order batting, but we will sort out the problems.
"England is a very good team, a very, very good team. They've been playing really good cricket for the last two years.
"If you are playing a world-class team, definitely, you play more positive cricket against England. So definitely, we will do so against England more positively."
Sri Lanka skipper Angelo Mathews was left to rue his team's dropped catches as Pakistan squeezed into the tournament's last four.
"If we had held those catches, it would have been a different story today, especially at very crucial times," he said.
"Towards the end, where we let go of Sarfraz, it was very crucial.
"We dropped three catches (Azhar Ali was put down in the first over), but there has been a genuine effort from the boys and from the support staff, and we've expressed a lot about our fielding and we've talked about it a lot. There has been a genuine effort on fielding.
"When you lose a game, you can always say it was poor batting, it was poor fielding, and it was poor bowling.
"Yes, we were 30-40 runs short, obviously, but I am really proud of the guys, the way they came out and tried to defend that. We had that in our bags, but unfortunately, the fielding let us down."