Madrid had taken a two-goal lead at halftime through Luka Modric and Endrick before the hosts rallied to even the match on Wednesday night.
The 20-year-old Garcia, a striker who usually plays with Madrid's "B" team, sealed the victory with a 93rd-minute header after a cross by Brahim Diaz.
"I'm thrilled, filled with emotions. It has been a dream since I was a child and since I joined the youth teams. I'm happy to have scored the goal that gave us a place in the semi-finals," Garcia said.
"It was a perfect cross by Brahim and the ball went in by itself."
Another setback would have been troublesome for Madrid after losing 1-0 at Espanyol in La Liga on Saturday for their second defeat in 15 matches across all competitions. Madrid had needed extra time to eliminate Celta Vigo at home in the round of 16 last month.
"We suffered but we are in the semi-finals of the Copa del Rey," Madrid coach Carlo Ancelotti said.
"We have to be satisfied with the outcome."
Ancelotti rested some regular starters ahead of the league derby against Atletico at the Santiago Bernabeu Stadium on Saturday. Madrid lead Atletico by one point at the top of the standings.
Madrid last won the Copa in 2022-23 and were eliminated by Atletico in the round of 16 last season.Â
Leganes only made it to the Copa last four in 2017-18, when eliminated by Sevilla.
Atletico had become the first team to reach the semis when they routed Getafe 5-0 at home on Tuesday with a pair of goals by Giuliano Simeone, the 22-year-old son of Atletico coach Diego Simeone.
In the other two quarter-finals on Thursday, it will be Real Sociedad hosting Osasuna and Barcelona playing at Valencia.