Pakistan kept their nerve to avoid a huge shock on Saturday, beating a spirited Afghan side by three wickets with just two balls to spare to keep their World Cup semi-final hopes alive.

All-rounder Imad Wasim hit Afghanistan skipper Gulbadin Naib for a boundary through the covers to seal the win off the fourth ball of the last over, prompting chaotic scenes as fans poured onto the Headingley pitch.

Pakistan, chasing a modest 228-run target, were in grave danger at 156 for six in the 39th over.

But man-of-the-match Imad (49 not out) and Shadab Khan (11) put on 50 for the seventh wicket to revive their hopes.

A third consecutive victory lifts Pakistan into fourth place in the 10-team table, above hosts England, with the top four qualifying for the semi-finals.

If they win their final game against Bangladesh and England fail to win both of their last two matches, against India and New Zealand, Pakistan are almost certain to progress.

"Thank you all the fans, they cheered every ball. It is a great win for us on not an easy pitch," said Pakistan captain Sarfaraz Ahmed.

"We all know that it is not easy to win our last four games," he said. "We go match by match. We will all be watching India v England tomorrow."

Pakistan Prime Minister Imran Khan, a World Cup-winning captain in his cricket days, tweeted his congratulations to the team for "keeping their nerve under pressure".

- Pressure -

Pakistan were in deep trouble when Sarfaraz was run out for 18 while taking a unnecessary second run, leaving Pakistan needing 72 off the last eleven overs.

They took 18 off Naib in the 46th over, which brought the target down to a manageable 28 off four overs.

Shadhab was run out for 11 with the score on 206 but Wahab Riaz (15 not out) and Imad saw Pakistan home and condemned plucky Afghanistan to an eighth consecutive defeat.

"When I went in they were bowling brilliantly but I just hung in there," said Imad. "We wanted to bat out the 50 overs and see.

"Gulbadin (Naib) was the only one who could be targeted. They have world-class spinners so we attacked him. Thank you to the crowd, it feels like home here. They gave us a boost in confidence."

Off-spinner Mohammad Nabi (2-23) had brought Afghanistan right into the game with the wickets of Imam-ul-Haq (36) and Babar Azam (45) in successive overs.

Haris Sohail laboured to 27 off 57 balls before he was lbw to leg-spinner Rashid Khan but Pakistan ultimately had enough batting firepower to see them over the line.

Earlier, lanky Afghan paceman Shaheen Shah Afridi shone with the ball, returning figures of 4-47 to add to the three wickets he took against New Zealand.

"We fight really well, the boys gave 100 percent but in the end we missed an opportunity to win the match," said Afghanistan captain Naib.

"Credit to Pakistan, they controlled their nerves.

"We missed again. You can face a lot of these situations, we are working harder and harder. The matches we have lost, we have improved."

The start of the game was marred by clashes between rival fans, with police forced to intervene. Political relations between the south Asian neighbours have been marked by years of distrust and blame.

And after the match, jubilant supporters swarmed onto the pitch.

An International Cricket Council spokesman said it was working with security officials and the police "to ensure there are no further incidents".

Feature image courtesy: Twitter / CricketWorldCup