The Alicante side had the better of the first half but could not make the breakthrough and a Ronaldo free-kick just after half-time saw them trail.
Madrid looked set to be denied right at the death though as Richmond Boakye headed winless Elche level in stoppage-time at the end of the game.
However, there was still just enough time for the visitors to find a winner and Carlos Sanchez's foul on Pepe gave Madrid the chance to do just that from the spot.
Ronaldo, who now has nine goals in his past five games, made no mistake to secure the win that sees Carlo Ancelotti's men move to within two points of joint Primera Division leaders Barcelona and Atletico Madrid.
Also on Wednesday, Sevilla eased the pressure on coach Unai Emery with a 4-1 victory over nine-man Rayo Vallecano that lifts them off the foot of the standings.
Ivan Rakitic's 17th-minute penalty put the hosts ahead and the Croatia international doubled Sevilla's advantage eight minutes into the second half as the Andalusian side chased a first league win of the season.
Rayo pulled a goal back two minutes later through Adrian Gonzalez but their hopes of getting back on terms suffered a blow in the 66th minute when they lost Alejandro Galvez to a second yellow card.
There was no way back for the visitors after that and Sevilla wrapped up the points with two goals from substitute Carlos Bacca in the final nine minutes, the second of which came against nine men after Rayo had seen Jonathan Viera shown a straight red card.
Valencia coach Miroslav Djukic also found himself in the firing line following a recent run of four successive competitive defeats but his side were celebrating back-to-back league wins after clinching a last-gasp 1-0 victory at Granada.
With a disappointing game seemingly drawing to a limp conclusion, Brazilian forward Jonas struck with almost the last kick of the match as Valencia followed up their 3-1 triumph over Sevilla with another victory.