The Wasps were well beaten by the Tynecastle side in the Championship in midweek but recovered to pull off their first competitive victory over the Jambos and book their place in the quarter-finals.
Hearts were well on top for most of the game against a side with just one point in five league games this season.
The visitors, who were struck a pre-match blow when defender Craig Halkett pulled out of the side during the warm-up with a back spasm, should really have taken the lead in the seventh minute.
With the Alloa central defenders forward for a set-piece, Olly Lee was allowed to scamper through a gaping hole at the heart of the home rearguard. He attempted to go round the advancing Neil Parry but the goalkeeper got a touch on the ball and when Liam Boyce tried to drill in from the ricochet Parry diverted the effort round a post.
Hearts were dominating possession but could not create anything as clear-cut as that early opportunity for Lee.
Walker rose free in the box to meet a Michael Smith cross from the right but his header was wayward in the 18th minute.
Hearts were back on top at the start of the second period and twice in quick succession Walker threatened. First Liam Dick cleared from under his crossbar as the attacker burst into the box and nine minutes later defender Nicky Jamieson pulled off a magnificent block to deny him.
Again, though, Alloa were able to soak up the pressure and they created their best chance 17 minutes from time.
Scott Taggart flung over an excellent cross from the right and Liam Buchanan's brilliant header looked to be sneaking in at Craig Gordon's left-hand post before the goalkeeper flung himself to touch it round the upright.
The woodwork played a part in the first period of extra-time when Parry could only touch a deflected cross from Hearts substitute Jordan Roberts on to the bar and the ball rebounded off fellow replacement Craig Wighton and dropped wide.
And just three minutes into the second half of the extra half-hour, Alloa finally found the breakthrough.
Taggart sent in a cross from the right that was allowed to drift through to Thomson, who crashed down under a challenge from Walker.
Referee Gavin Duncan pointed to the spot and Trouten sent Gordon the wrong way to find the bottom right corner of the net to secure a stunning win.