Rangers looked on course for a comfortable victory in the semi-final tie at the Indodrill Stadium when they took a 2-0 lead thanks to second-half goals from Kenny Miller and Dean Shiels.
However, quickfire goals from Greig Spence and Ryan McCord levelled it up at 2-2 before Spence struck again with a minute remaining to secure a famous victory and a meeting with Livingston in April.
There was an uneventful start to the game but Rangers suffered a blow in the 10th minute when young midfielder Lewis Macleod was stretchered off with what appeared to be a hamstring problem, with Shiels coming on in his place.
Alloa had a great chance to take the lead four minutes later but Spence failed to connect properly with Ben Gordon’s header and Steve Simonsen easily gathered the ball in the visitors’ goal.
Adam Asghar then fired wide for the hosts, while Nicky Law and Kris Boyd were both off target at the other end.
Liam Buchanan had another good opportunity for Alloa in the 37th minute when the ball fell into his path 12 yards out after Spence’s effort was blocked, but he dragged his shot to the left of the goal.
Shiels headed wide for Rangers shortly after the restart but the deadlock was broken in the 49th minute when Miller was on hand to slot home from 10 yards after Alloa ‘keeper Craig McDowall could only parry Boyd’s low, 25-yard free-kick.
Darren McGregor then forced a good save from McDowall, while Simonsen had to be alert for Rangers to keep out Buchanan’s cut-back as it deflected off Bilel Mohsni.
The game looked to be over in the 64th minute when Rangers went 2-0 up through Shiels’ well-placed strike after he was teed up by Miller.
However, Spence pulled a goal back in the 72nd minute with a close-range header from Mark Docherty’s centre and Alloa were level just two minutes later after McCord wriggled free in the box and fired past Simonsen from Edward Ferns’ right-wing cross.
McDowall did well to push substitute Jon Daly’s close-range header over as Rangers attempted to hit back but Alloa stunned their Championship rivals in the 89th minute when Spence stroked home a side-footed shot from 14 yards from Steven Hetherington’s centre from the right.