Imad Wasim hit a boundary to finish off a three-wicket win with two balls to spare against Afghanistan and lifted Pakistan above England into fourth spot on the Cricket World Cup standings.
Wasim finished unbeaten on 49 with Pakistan on 230-7 and shared important lower-order partnerships of 50 with Shadab Khan (11) and 24 with No. 9 Wahab Riaz, who was unbeaten on 15.
With Pakistan needing 16 runs off 11 balls, Wahab hit a six off Rashid Khan and then ran a risky two — despite the two unnecessary run outs of Shadab and captain Sarfaraz Ahmed that compounded pressure earlier in the innings.
A small group of fans spilled onto the field at Headingley to congratulate the players, waving flags and causing all kinds of issues for the security.
Teenage paceman Shaheen Afridi took 4-47 earlier as Pakistan restricted Afghanistan to 227-9 in an important start to the game. It was a record eighth consecutive loss for Afghanistan and a second narrow defeat to a regional rival — it followed an 11-run loss to two-time champion India.
Pakistan, after being trounced in its opening game, has beaten South Africa, New Zealand and Afghanistan to move into the semifinal positions on nine points from eight games, one ahead of England. The host team plays India on Sunday.
It was a seesawing game at Leeds.
After losing Fakhar Zaman on the second ball of the run chase, trapped lbw by Mujeeb Ur Rahman, Pakistan was coasting with Imam-ul-Haq (36) and Babar Azam (45) sharing a 72-run second-wicket stand until Mohammad Nabi struck twice to remove both established batters in consecutive overs.
Imam was stumped on the last ball of the 16th over when he charged Nabi, missing a quicker, wider ball and was easily out of his ground when wicketkeeper Ikram Ali Khil whipped off the bails.
Babar attempted to sweep a fuller delivery at the start of the 18th over and was bowled.
Still, they were in a comfortable position at 103-3 halfway through the overs until wickets started dropping and the run-rate escalating.
Haris Sohail had a reprieve at the end of the 28th over when Afghanistan appealed loudly for a leg-side caught behind, but umpire Nigel Llong ruled not out and neither team had any referrals left. TV replays showed there was a fine edge.
But just when all the momentum seemed to be going entirely Pakistan's way, 18-year-old Mujeeb started a new spell in the 30th over and immediately had Mohammad Hafeez (19) caught at point.
Sohail labored to 27 from 57 deliveries before being trapped lbw by Rashid Khan, making it 142-5 in the 35th, and captain Sarfaraz's inexplicable run out for 18 in the 39th made it 156-6. That left Pakistan needing 72 off 11 overs with only four wickets in hand.
The Pakistani seventh-wicket pair took 18 runs off the 46th over to cut the asking rate to 28 from 24 balls.
But Shadab's run out in the 47th over again set up a tense finish.
The neighboring countries were meeting for the first time at a World Cup.
The International Cricket Council said it was aware of "some scuffles" outside Headingley and was working with police and security. There were reports on social media of skirmishes involving opposing fans outside the stadium, and of at least two fans being ejected from the game.
The Alternative Commentary Collective are podcasting their way through the World Cup. Known for their unconventional sports analysis and off-kilter banter, the ACC have come to ask the tough questions. Here's the latest episode of 'The Agenda':
WARNING: GRAPHIC CONTENT