Danny McGuire ran in two late tries as Leeds, Challenge Cup runners-up for the last three seasons, eventually saw off spirited Castleford at Headingley.
Leeds trailed 6-4 at the break and were still behind after an hour when Jamie Ellis scored Cas's second try to put his side back in front at 12-10.
But Leeds, who had scored their earlier two tries from Kallum Watkins and Rob Burrow, finished strongly.
Chris Clarkson put them ahead again before McGuire's double sealed it.
Rangi Chase was also on the scoresheet for Cas, with a first-half try, his first try of the season.
But, as he continues to be linked with an imminent return to Australia, fuelled by the reception he was accorded at the final whistle, it was a night which looks to have been his last in Tigers' colours.
Kevin Sinfield kicked four of his five goals at Headingley to head above Kel Coslett into sixth in the list of all-time point scorers.
The Tigers had only beaten Leeds twice in nine previous Challenge Cup meetings - but their most recent exit to the Rhinos was in the semi-final two years ago when they only lost 12-10 to a sudden-death Kevin Sinfield penalty.
And, having guided the Tigers to a first win in eight games against Widnes a week earlier, caretaker coach Danny Orr's much-improved side made the Rhinos battle for their place in Monday's fifth round draw.
Evergreen Leeds and England prop Jamie Peacock, 10 years on from being voted Man of Steel in 2003, once again led from the front with an heroic display to help keep the 11-times winners in the Cup.
But it was McGuire who came to the Rhinos' rescue, getting Chris Clarkson over for a short-range try on 61 minutes before twice skipping past tiring defenders for two of his own.
VIEW FROM THE DRESSING ROOM
Leeds coach Brian McDermott:
"Jamie Peacock has been absolutely immense this year for us. I know it's far too early to be talking about Man of Steel but, if you were to choose it now, I'm sure he would be among the top one or two contenders.
"Had we converted some of our early scoring opportunities into tries, we may have had more breathing space but I still think Cas would have thrown everything at us.
"They were the factor in how competitive that game was. We needed to be very good defensively to stop them."
Castleford caretaker coach Danny Orr:
"I can't fault their effort. It was absolutely outstanding. To come here and play the way we did, I'm actually quite proud of them.
"The first half went according to plan. We knew they would come out strong but we weathered the storm and got on top. They probably raised their game a bit in the second half and we didn't go with them.
"We gifted them a lot of field possession and, if you do that against Leeds, they will punish you. Danny McGuire really stepped up."
FRIDAY NIGHT'S LINE-UPS
Leeds: Hardaker; Watkins, Ablett, Moon, Hall; Sinfield, McGuire; Leuluai, Burrow, Peacock, Ward, Delaney, Jones-Buchanan.
Replacements: McShane, Kirke, Clarkson, Achurch.
Castleford: Tansey; Clare, Shenton, Thompson, Carney; Chase, Ellis; Mason, Milner, Huby, Holmes, Hauraki, Massey.
Replacements: Walker, Clark, Gilmour, Boyle.
Referee: Phil Bentham (Warrington).