Ronaldo had put the visitors ahead from a free-kick early in the second-half, but Richmond Boakye thought he had rescued a point when he headed home in the 91st minute before the late drama.
Elche had also started the brighter side and Real had goalkeeper Diego Lopez to thank for preventing his side from falling behind for the fourth time in six league matches this season.
David Lomban was first to be denied by Lopez as he parried the defender’s header from Eduardo Albacar’s cross before Carles Gil’s low drive was also smothered by the keeper.
Javi Marquez also fired wastefully over as the hosts continued to press and they were rightfully infuriated when Fernandez awarded a foul when Ramos chopped down Coro but didn’t show the Spain international the 18th red card of his career.
Madrid started the second period with more purpose and were rewarded just six minutes after the restart when Ronaldo’s free-kick bounced just in front of Manu Herrera and he could only help it on its way into the net.
Having gone in front though, Madrid took their foot off the gas and were finally punished in stoppage time when Elche produced their best move of the match to equalise.
A long ball from Herrera was headed into the path of Fidel and his cross was expertly nodded home at the near post by Boakye.
However, the drama didn’t end there as having signalled for there to be three minutes of added time, Fernandez allowed a Madrid corner to be taken well into the 94th minute and then pointed to the spot when Pepe hit the ground.
After a number of Elche players had been booked for protesting, Ronaldo kept his cool and drilled the ball low into the corner to earn his side a precious three points.
In the night’s earlier matches, Sevilla moved off the bottom of the table as they beat Rayo Vallecano 4-1 to secure their first victory of the season.
Ivan Rakitic and Carlos Bacca each scored twice for the hosts with Adrian Gonzalez netting for Rayo, who also had Alejandro Galvez and Jonathan Viera sent off.
And Valencia’s mini revival continued as Jonas Goncalves struck with virtually the last kick of the game to hand them a 1-0 win at Granada and move Miroslav Djukic’s men up to seventh.