Hearts are on the cusp of the Betfred Cup last-16 after a double from defender Craig Halkett secured a 2-1 win over Stenhousemuir.
The Jambos looked destined to suffer a humbling defeat when Mark McGuigan gave the Ladbrokes League Two visitors a shock lead 13 minutes from time.
However, Halkett notched two goals in the last seven minutes to spare the hosts' blushes and ensure they will top Group A providing they do not lose to East Fife on Saturday.
Hearts were unable to find a way past their resolute visitors despite dominating the first period, with Graeme Smith making fine saves to deny Steven MacLean, Sean Clare and Michael Smith.
Stenny, playing for pride following successive defeats against East Fife and Dundee United, turned in an even more heroic defensive showing after the break.
In a remarkable sequence of play, Graeme Smith made a stunning double save to deny Hearts' Michael Smith and Ollie Bozanic, before defender Scott McLaughlin made two quickfire blocks as Jamie Walker and Conor Washington sought to tap into an empty net.
The Ochilview outfit stunned Tynecastle after 77 minutes when they broke the deadlock against the run of play, with McGuigan showing admirable predatory instincts to poke home an Alan Cook cross from close range.
However, Halkett rode to the rescue by prodding home a leveller from close range, before the former Livingston skipper made it three goals in two appearances for the Jambos when he headed home a fine Jake Mulraney cross with just two minutes left on the clock.