Los Blancos headed into the second leg with 1-0 advantage after the reverse fixture but Leganes started firing on all cylinders as Claudio Beauvue almost opened the scoring in the fifth minute with a strike that came off the woodwork.
Los Blancos recovered after 15 minutes and found joy on the wings but their delivery into the box lacked the required quality.
Tito had a chance to find the breakthrough for the visitors in the 23rd minute after being left unmarked inside the box but his header sailed over the bar.
Eraso opened the scoring for Leganes on the half-hour mark with a screaming shot from outside the box that peached into the top corner, 0-1 and 1-1 on aggregate.
Beuavue almost sensationally got the second goal for the visitors five minutes before the break with an acrobatic over-head kick that sailed slightly wide.
The first half ended with the teams tied on aggregate, although Leganes were leading in the match.
Karim Benzema equalised for Zinedine Zidane's troops two minutes after the restart with a delicate chip at the near post to make it 1-1 and 2-1 Los Blancos on aggregate.
However, Gabriel restored the lead for the visitors in the 55th minute with a thumping header inside the box to make it 2-2 on aggregate.
The chances were providing hard to come by for the hosts after 65 minutes as Leganes were fired up in their defensive duties.
Zidane's men were still pedestrian with five minutes left on the clock and lacked the creative spark going forward.
Benzema had a chance to get his second goal two minutes from time but missed from close range following a corner as Leganes held on for a famous result at the Santiago Bernabeu to advance to the semi-finals.