You are not permitted to download, save or email this image. Visit image gallery to purchase the image.
A day after Sri Lanka stunned England in Leeds, an even bigger upset looked on the cards only for India to sneak home by 11 runs after strike bowler Shami's last-gasp heroics.
Afghanistan made the most of a slow wicket to restrict India to 224-8, their lowest total of the tournament, but then struggled themselves. Needing 16 to win off the last six balls with three wickets remaining, they were all out for 213, slumping to their sixth successive defeat.
Shami dismissed Mohammad Nabi, who top-scored for the Afghans with 52, Aftab Alam and Mujeeb Ur Rahman to complete his hat-trick.
"Shami was really good today. He was making the ball move more than anybody," India captain Virat Kohli said of the quick, who was playing his first match of the tournament.
"This game was way more important for us, because things didn't go as planned. That's when you need to show character and bounce back."
Having bled 397 in their previous outing against England, the spin-heavy Afghan attack had earlier redeemed themselves against India's vaunted batting line-up.
Off-spinner Mujeeb drew first blood in the fifth over when he bowled the in-form Rohit Sharma for one after India opted to bat.
India, known for preserving wickets and accelerating later, managed only 41 runs in the first 10 overs.
Kohli batted with trademark fluency, though, making up for a lack of boundaries with smart running between the wickets during his breezy 67.
He forged two half-century partnerships, first with KL Rahul and then Vijay Shankar, to steady the innings.
Replacing the injured Shikhar Dhawan at the top of the order, Rahul made 30 before attempting an unnecessary reverse sweep and gifting his wicket. Shankar did not convert his start either and made 29 before falling leg-before to Rahmat Shah.
Nabi then struck the biggest blow in the 31st over when he had Kohli caught at point.
India still had enough firepower in their batting line-up but the unerring Afghan spinners straight-jacketed them. Mahendra Singh Dhoni made a laboured 28 and Hardik Pandya could not reproduce his trademark power-hitting.
Kedar Jadhav (52) hit the only six in the India innings but the expected late burst did not materialise.
Rashid Khan, whose figures of 9-0-110-0 against England were the worst in World Cup history, bounced back with 1-38 in 10 tidy overs.
Afghanistan wobbled early in their chase, losing Hazratullah Zazai in the seventh over but they made slow, steady progress even after Gulbadin Naib fell for 27.
Jasprit Bumrah dismissed Rahmat Shah (36) and Hashmatulla Shahidhi (21) during a two-wicket maiden to drag India back into the contest.
Shami conceded a boundary off the first ball of the final over but struck with his third, fourth and fifth deliveries to finish with 4-40.
"The total was not that much (to chase) but in the middle we needed some 80s and 100s because 20s and 30s aren't enough against a team like India," Afghan captain Naib said.
"When chasing around 220 you need some batsmen to take responsibility and go longer.
"We have lost four games very badly, but the last two games we have played really well. This is tough cricket against high-ranking teams, and you have to fight your best."