Experienced defender Gonzalo Verdu struck the free kick into the wall but his second effort deflected off Dani Ceballos and wrong-footed goalkeeper Andriy Lunin before ending up in the back of the net.
Real grabbed the equaliser when Isco deflected a Ceballos shot inside the box and substitute Eden Hazard scored the winner on a counter-attack after receiving a through ball from David Alaba, rounding the goalkeeper and finishing into an empty net.
The match ended in controversy as Elche found the net in the last minute of extra-time, but the referee disallowed the effort for a foul in the build-up.
After the long trip home from Saudi Arabia where Real won the Spanish Super Cup final against Athletic Bilbao 2-0 on Sunday, manager Carlo Ancelotti rested several first-team regulars.
“Hazard and Isco won the game, we have to highlight that. That means something,” Ancelotti said. “Maybe they deserved to play more, but the past is the past, and we have to look forward. I can count on them, and a squad that has an extraordinary character, it never gives up, fights to the end against adversity.
“It’s the game from this period of the season that has given me the most happiness.”
“I said to the players before the game that it would be a long game, they’ve done well, we have to be ready for everything, for mistakes, we could concede, it could go to extra time, we had to be prepared. I knew the changes could give a boost to the team and they did.”
“It’s a big accomplishment to come-from-behing down to 10 men,” Real defender Nacho Fernandez told reporters.
“It was a long game but they have defended really well. We are happy. We fought until the end, even with adversity. I think we deserve to advance.”