The Portuguese forward scored in the final minute of the match to save the visitors from dropped points against the newly promoted side
After a goalless first half at the Estadio Manuel Martinez Valero, it did not take Madrid long to find the lead as Elche became the 27th different La Liga side which Ronaldo has scored against.
Elche thought they had rescued a point in stoppage time when Richmond Boakye equalised, but Ronaldo buried an even later penalty to give Real a fortuitous win.
The hosts went into the match as La Liga’s only winless team this season and coach Fran Escriba made three changes to the side which lost to Osasuna last time out.
Miroslav Stevanovic, Fidel Chaves and Richmond Boakye sat out, with Manu Del Moral, Carles Gil and Javi Marquez taking their places.
Real Madrid hammered Getafe 4-1 at the weekend and their team on Wednesday included three alterations. Nacho, Daniel Carvajal and Asier Illarramendi were replaced by Fabio Coentrao, Ramos and Luka Modric.
The hosts went into the match as underdogs but began the encounter impressively and only failed to take an early lead thanks to an excellent save from Diego Lopez as he tipped Lomban’s header wide.
Valencia loanee Gil looked lively and produced a lovely chip to the back post for Javi Marquez, but again Lopez rescued the Real defence with a smart save.
Ancelotti’s side grew into the contest after the 20th minute and Ramos should have opened the scoring, but the unmarked defender could only head Cristiano Ronaldo’s free kick at Manu Herrera.
Although Elche continued to look promising going forward, Madrid should have ended the first half in the lead, but Herrera made another important stop, this time parrying Angel Di Maria’s low effort from the left.
Madrid wasted their chances in the first half, but seven minutes into the second period Ronaldo broke the deadlock as his powerful free kick took a deflection off the wall and squeezed past Herrera.
The hosts were enraged soon later when the referee failed to give them a penalty for Sami Khedira’s tackle on Damian Suarez, but the Uruguayan was correctly given a yellow card for diving.
The final 15 minutes saw Elche press for an equaliser and after Ramos missed another headed chance, substitute Boakye flicked a cross past Lopez at the front post as a draw looked certain.
However, the referee made a controversial call to penalise Carlos Sanchez for a tug on Pepe in the penalty area, and Ronaldo fired home from 12 yards to secure the three points in dramatic fashion.