Carl McHugh's late winner sent Hearts out of the Scottish Cup and earned Motherwell a place in next month's semi-finals.
With the scoreline at 1-1, McHugh chested down a clearance and volleyed high beyond Hearts goalkeeper Jon McLaughlin to decide the tie on 87 minutes. Curtis Main scored inside seven minutes for Motherwell but Kyle Lafferty's second-half penalty restored parity before McHugh's winner.
Hearts had looked revitalised after the break and Lafferty hit the post after converting his penalty. Exiting the competition so late in the game was sore for Hearts, who looked set to take the game to a replay back at Tynecastle, but Motherwell celebrated joyously at full-time.
This competition represented the last chance of a trophy for both clubs this season so it was no surprise to see such a competitive affair often interrupted by fouls. Hearts gave 20-year-old Canadian Marcus Godinho a debut at right wing-back and switched Connor Randall to the left with Demetri Mitchell injured. Motherwell retained the same starting line-up from their 1-0 win at Dundee last weekend.
In front of around 4,000 travelling fans, the hosts had the ball in the net inside three minutes. Referee Andrew Dallas blew for a foul as the visiting goalkeeper Jon McLaughlin had a hand on the ball when Craig Tanner poked it out of his grasp and over the line. There was no dispute - just sheer confusion - when Motherwell did take the lead just moments later.
Hearts midfielder Ross Callachan curiously allowed Tanner's low corner to drift beyond him at the front post without attempting to clear. McLaughlin reacted but could only prod the ball against the metalwork. Instinctively, Main flicked his foot at the rebounding ball and managed to force it over the line for one of the season's most freakish goals.
Steve Robinson, the Fir Park manager, would have been pleased seeing his side deservedly in the lead. They were the better team throughout the first half as Hearts struggled for cohesion, other than a couple of combinations between forwards Steven Naismith and Lafferty.
Callachan was replaced by David Milinkovic at the interval and instantly Hearts looked more dangerous. They won a penalty in the 51st minute when Naismith exchanged passes with Randall and sprinted into the Motherwell box. He was fouled by defender Cedric Kipre's rash challenge and Lafferty rammed the resultant spot-kick firmly down the middle to level the scoreline at 1-1.
Seconds later, the Northern Irishman drove an angled shot across goal which rebounded off the far post. Trevor Carson then had to dive full length to his left to keep out Lafferty's glancing header from Harry Cochrane's cross. The re-energised visitors were now in the ascendancy.
Motherwell looked unsettled but recovered to play their part in a very open last 20 minutes. Both teams sensed this tie was there for the taking and play began swinging frantically from end to end. Eventually, the hosts forced a winner with just three minutes of regulation time left.
McHugh chested down Christophe Berra's header almost 25 yards out and volleyed an unstoppable effort high beyond the despairing dive of McLaughlin. As Fir Park erupted, there was little time left for Hearts to score again and the final whistle signalled Motherwell's progress to the last four.
Motherwell (3-4-1-2): Carson; Kipre, Aldred, Dunne; Cadden, Campbell, McHugh, Tait (Frear 78); Tanner (Rose 90); Main, Ciftci (Bowman 55).
Unused subs: Griffiths, Bigirimana, Newell, Maguire.
Hearts (3-5-1-1): McLaughlin; Souttar, Hughes (Amankwaa 88), Berra; Godinho, Callachan (Milinkovic 46), Adao, Cochrane, Randall (M Smith 90); Naismith; Lafferty.
Unused subs: Hamilton, Buaben, Cowie, Henderson.
Referee: Andrew Dallas.