Afghanistan, after losing their opening three qualifying games, suffered a middle-order wobble that looked to have handed Ireland the advantage.
But Stanikzai, who received on-field treatment for dressings around his abdomen, hit an invaluable 39 not out off 29 balls to guide the Afghans to 213 for five in reply to Ireland's 209-7.
"I am in too much pain, but I was playing for the country," said Stanikzai. "It is our dream to qualify for the World Cup."
Afghanistan will join fellow qualifiers West Indies in the 10-team World Cup to be staged in England and Wales from May 30 to July 14, 2019, alongside defending champions Australia, India, Pakistan, Sri Lanka, Bangladesh, England, South Africa and New Zealand.
After Scotland and Zimbabwe had both suffered defeats in their concluding Super Six matches earlier in the week, Friday's game at the Harare Sports Club suddenly became a winner-takes-all clash.
Ireland won the toss and elected to bat with opener Paul Stirling making a top score of 55 and Kevin O'Brien hitting 41 in a modest total of 209-7 off their 50 overs.
Afghanistan leg-spinner Rashid Khan finished with 3-40 to take his career tally to 99 ODI wickets.
In reply, burly opener Mohammad Shahzad clobbered 54 off 50 balls in an opening stand of 86 with Gulbadin Naib.
But once Shahzad holed out, Afghanistan, who only squeezed into the Super Sixes after losing to Scotland, Zimbabwe and Hong Kong, began to wobble.
But skipper Stanikzai and Najibullah Zadran, the latter finishing on 17 not out, settled the side and led the Afghans to a memorable victory with five balls to play.
"We lost a couple of matches, but we played good cricket in the Super Sixes," said man-of-the-match Shahzad.
"We had to win this game."
Ireland captain William Porterfield was left in tears after the game.
"First of all, congratulations to Afghanistan," he said. "We were probably 25 runs short.
"Then again in games like this we can't fault anyone.
"We had opportunities but we lost three games. We had some key moments, but could not take it."
Ireland: 209/7 off 50 overs (Stirling 55, L O'Brien 41; Rashid Khan 3-40)
Afghanistan: 213/5 off 49.1 overs (Mohammad Shazad 54, Gulbadin Naib 45, Stanikzai 39*; Simi Singh 3-30)
First Published: March 23, 2018, 8:40 PM IST