With six minutes left they trailed bottom of the table Livingston and all hopes of catching city rivals Hibs for third place seemed to be ebbing away. But then top scorer Paul Hartley curled home a spectacular free-kick for his 13th goal of the season and with Livingston still shell-shocked Joe Hamill's cross was nodded in by recent signing Lee Miller.
That gave Hearts their first away win since November and keeps Hibs just about within sight, eight points ahead. But this was a bitter pill for Livingston manager Richard 'Van' Gough to swallow as they suffered their fifth defeat in six league games.
An early Burton O'Brien shot which was deflected into the arms of keeper Craig Gordon was their only contribution to an opening phase which Hearts dominated without threatening to score.
Hartley's 25-yarder was easily saved and former Hearts reserve Roddy McKenzie was soon called into action again after Robbie Neilson's driving run and pass set up Stephen Simmons for a low angled shot.
Youngster James McPake failed to find the target in a rare Livingston counter-attack but Hearts were back in control for the rest of the first half although long-range headers from Simmons and Andrew Webster were the closest they came to underlining their superiority.
Terrific play from Hartley ripped open the home defence at the start of the second period but Simmons was unable to capitalise and blasted wildly over the top.
Despite struggling with a knock which forced him out of the game moments later Webster went close with another header as Hearts kept pressing.
Livingston striker Derek Lilley then stunned the visitors when he scrambled the ball into the empty night after O'Brien's chip had unbalanced the Hearts defence and dragged Gordon out of position.
Hearts almost equalised when Jason Dair cleared a Steven Pressley header off the line and Miller also had a shot smothered by McKenzie.
Only a world-class save from Gordon to push away a freak cross-cum-shot from substitute Craig Easton kept the Edinburgh side alive and set the scene for their late fightback.