The World Cup debutants appeared to be heading for a heavy defeat after a middle-order collapse left them on 97 for seven in pursuit of Scotland’s 210 all out - their highest ever total in cricket’s premier global competition.
But Shenwari almost single-handedly kept Afghanistan’s hopes alive and he rapidly progressed after passing his half-century before eventually falling four runs short of his hundred, leaving Afghanistan still 19 runs adrift of their 211 target.
Hamid Hassan and Shapoor Zadran steadily accumulated, though, and, despite a few brief nervy moments including a near-run-out from what turned out to be the penultimate delivery, got Afghanistan over the line with three balls to spare.
Zadran, who had earlier taken four for 38, struck the winning runs as Afghanistan claimed a first ever World Cup win at the third attempt while Scotland’s search for theirs goes on after an 11th consecutive defeat across three editions of the tournament.
Scotland were invited to bat first against a side they had lost to five times in eight previous one-day internationals, and they seemed to hand Afghanistan the initiative as they lost wickets at regular intervals in bowler-friendly conditions.
Disciplined bowling from Afghanistan, who only became an affiliate member of the International Cricket Council in 2001, helped them to reduce their fellow associate nation to 95 for five and then 144 for eight, with Dawlat Zadran taking three for 29.
But instead of sticking to the formula that had worked, they were perhaps guilty of taking their foot off the gas and allowed Majid Haq and Alasdair Evans to build a partnership.
The ninth-wicket pair accumulated steadily and their partnership of 62 from 75 balls took Scotland to their best ever World Cup total, eclipsing the 186 for eight they made against South Africa in 2007.
Haq contributed 31 and Evans added 28 before both were removed in the final over by Shapoor Zadran.
Evans carried his momentum into his bowling by cleaning up Nawroz Mangal in his second over before having Asghar Stanikzai caught behind three deliveries later.
Opener Javed Ahmadi appeared in fine touch, bringing up his half-century at better than a run a ball shortly after hitting his eighth four, yet his dismissal for 51, skying a catch to Matt Machan off Richie Berrington, heralded the start of an astonishing collapse.
From 85 for two, Afghanistan lost their next five wickets for the addition of 12 runs, with Berrington taking three wickets and Josh Davey two.
The writing seemed to be on the wall for Afghanistan but just as they had done when Scotland were batting, Preston Mommsen’s men appeared to ease a little.
Shenwari, who watched all the carnage unfold from the non-striker’s end, steadied the innings alongside Dawlat Zadran and then Hassan (15 not out off 39 balls), bringing up his half-century off 113 balls.
Victory for Afghanistan still did not seem a serious prospect until Shenwari belted three sixes off Haq in the 47th over to move into the 90s, yet the bowler would have his revenge when the 28-year-old went for one heave too many and was caught on the boundary.
There were further twists in the tail, though, as last-wicket pair Hassan and Shapoor Zadran kept bringing the required figure down, culminating in a tense final over.
Afghanistan needed five runs from the final six balls, bowled by Iain Wardlaw, and got off to a solid start with a thick outside edge to deep third man from Hassan.
The second ball could have won it for Scotland, as Shapoor set off for a run that was never there after nudging to the leg side and afforded Machan a run-out opportunity, but the Sussex man’s under-arm throw was off target.
And, from the next delivery, Shapoor clipped Wardlaw off his legs for four to spark Afghan celebrations.
SCOTSMAN TABLET AND MOBILE APPS