Hooker Brad Dwyer kicked a long-range drop goal in extra time for Leeds' second win of the Betfred Super League season with a 21-20 success against Castleford.
The Rhinos had led 20-4 after 52 minutes but the Tigers looked set to complete a remarkable fightback when they scored three tries in 11 minutes to tie the scores.
Substitute full-back Calum Turner could have won it with a conversion while stand-off Jake Trueman had one drop goal disallowed for an obstruction and was wide with a second as the game went into golden-point added time.
Leeds just about deserved the result after dominating the first half but the relief was palpable as Dave Furner's men claimed their first win of the season at home and their first over Castleford at Headingley for six years.
Reeling from a 42-12 home defeat by St Helens, the Tigers were without stand-in scrum-half Jordan Rankin, which meant a switch from the pack for ex-Leeds hooker Paul McShane.
And McShane never looked comfortable in his new role, as he gifted Leeds early possession with a knock-on and a needless penalty while in possession.
Castleford coach Daryl Powell also re-shuffled his three-quarter line and Greg Minikin was handed the short straw after finding himself up against Tongan powerhouse Konrad Hurrell.
The former New Zealand Warrior barged four defenders, including Minikin, out of his way to score the first try, had a second disallowed for obstruction and sucked in two defenders before sending his winger Ash Handley over for his ninth try of the season.
Liam Sutcliffe, once more preferred at stand-off to Tui Lolohea, had the misfortune to strike each of the posts with his conversion attempts before finally hitting the mark with a penalty just before half-time to extend his side's lead to 10-4.
Castleford had drawn level when prop Liam Watts, fresh from his call-up by England coach Wayne Bennett, dummied his way over on the last tackle for a 22nd-minute try but the Rhinos generally had an answer for their opponents' laboured attack.
Powell shuffled his side at half-time, bringing on Turner at full-back and moving Peter Mata'utia to stand-off, which enabled McShane to revert to hooker.
Turner immediately showed his potential when he was called on to make a crucial tackle on centre Kallum Watkins after the Leeds captain picked up a loose ball 20 metres from his own line.
The tackle was good but the visitors were unable to regroup in time to prevent Sutcliffe taking a return pass from Walker to touch down.
Prop Trent Merrin then crashed over for the Rhinos' fourth try after 52 minutes and Cameron Smith, taking over the kicking duties from Sutcliffe, added the conversion to make it 20-4.
Castleford began their fightback when Minikin sliced through the Rhinos defence on the hour and three minutes later McShane's pinpoint pass enabled former Leeds forward Chris Clarkson to score against his old club.
Turner kicked both conversions to cut the deficit to four points and it was all square after 71 minutes when another former Leeds man, second rower Alex Foster, won the race to Mata'utia's grubber kick on the last tackle.
Turner missed a chance to put Castleford in front for the first time and Trueman had two chances to win the game with drop goals before Dwyer had the final say in extra time.