Afghanistan's innings didn't get early momentum as Shami got rid of Hazratullah Zazai for 10.
But it was propelled by two key partnerships as skipper Gulbadin Naib (27) added 44 with Rahmat Shah (36).
The latter then added 42 runs with Hashmatullah Shahidi (21). Man-of-the-match Jasprit Bumrah (2-39) got rid of Shah and Shahidi in the 29th over, starting his second spell with a double-wicket maiden.
Hardik Pandya (2-51) had dismissed Naib. "When you run behind wickets, you don't get wickets. We wanted to create pressure and take the run rate high. It was a tight game and I backed my skills," Bumrah said.Pandya put in a serious shift as India used only five bowlers.
Surprisingly, its much celebrated wrist spinners Kuldeep Yadav (0-39) and Yuzvendra Chahal (2-36) paled beside the Afghanistan spinners. Bumrah's spell proved to be the turning point.
India struck at regular intervals thereafter and never let the chase settle down. Afghanistan did well to take the match to the last over, making India sweat to the end and keep its fans on edge for the entire day.
Nabi's half-century came up off 53 balls as he put on 36 runs for the sixth wicket with Najibullah Zadran (21) and raised visions of a shock upset. India relied on match-winning experience. Pandya struck for a second time to get rid of Zadran in the 42nd over. Afghanistan's hopes were pinned on Nabi, and he maneuvered the chase to the last over, until holing out off Shami. It precipitated the final over collapse that saw Shami join an elite list in World Cup cricket.
India, unbeaten after five games, was third in the standings.Afghanistan has lost all six of its games.Earlier, Rashid Khan, who conceded a World Cup-record 0-110 against England, atoned against India with figures of 1-38 from 10 overs. In all, Afghanistan's spinners picked up five wickets as India's batsmen never really got momentum going on a tricky pitch.
Skipper Virat Kohli top-scored with 67 off 63 balls, including five boundaries, and was the best batsman on display.
India's uncharacteristic innings never got rolling. Mujeeb Ur Rahman (1-26) bowled in-form Rohit Sharma (1) in the fifth over, ending the freewheeling opener's run of two centuries and a half-century.Lokesh Rahul (30) put on 57 runs with Kohli for the second wicket but triggered India's problems when he was out caught to an unnecessary reverse sweep.
The Afghans took wickets at regular intervals.Vijay Shankar was trapped lbw by Rahmat Shah in the 27th over on 29Kohli batted as if on a different pitch, though, finding gaps well, rotating the strike, and stroking the occasional boundary to reach his 51st ODI half-century from 44 balls.
The big moment came when he was caught off Nabi (2-33) at short third man in the 31st over.Mahendra Singh Dhoni (28) and Kedar Jadhav (52) stabilized the innings with 57 runs for the fifth wicket.
But Dhoni faced 52 balls and his innings ended tamely in the 45th over when he was stumped for only a second time in an ODI since 2011. India lost Pandya (7) and Shami (1) before the end. Jadhav's 66-ball half-century was the saving grace of India's final 10 overs as the No. 2-ranked team was restricted to just 49 runs.
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