A late winner from Canadian defender Kevin McKenna saw third placed Hearts come from behind to break their Almondvale jinx and move nine points clear of Dunfermline in the race for a UEFA Cup spot.
The Edinburgh club's first ever SPL win in West Lothian also gave them their third victory in four outings since going out of the cup to Celtic.
This was only Livingston's third home defeat of the season and leaves them still just inside the top half of the table, but struggling for form barely a fortnight before the CIS Cup Final with Hibs.
Hearts' poor record at the City Stadium had looked set to continue after former Tynecastle midfielder Lee Makel stunned his old club in the fifth minute with a low free-kick from just outside the penalty box.
Makel's ninth goal of the season put Livingston firmly in control, but the visitors managed to work their way into the game and drew level in the 26th minute when Robbie Nielson's cross found Mark De Vries rising above Marvin Andrews to direct a precise header past keeper Roddy McKenzie.
The equaliser ended the big Dutchman's recent scoring drought and heralded a bright spell for Hearts with Alan Maybury and Phil Stamp both going close, before McKenzie denied Andy Kirk shortly before half-time when the Irish striker hammered in a vicious dipping volley.
Kirk got the better of his old team-mate with a curling effort two minutes into the second period, although the lead lasted only nine minutes as Colin McMenamin made it 2-2 with a simple finish from six yards.
A point apiece seemed inevitable and both teams might have settled for that, but with six minutes left, Hearts struck the fatal blow when youngster Robert Sloan swung in a corner kick and, just three days after hitting the target against Dundee United, Mckenna confirmed his ability for scoring important goals by guiding a trademark header past McKenzie.