The Portugal forward smashed home a free kick to put Real ahead in the 51st minute against a plucky Elche side before substitute Richmond Boakye looked to have secured a point when he headed past Diego Lopez in the 91st minute.
There was more drama to come, however, as Real won a final corner and the referee pointed to the penalty spot after Pepe tangled with midfielder Carlos Sanchez in the area.
Television replays showed the pair grappling with each other before tumbling together to the floor and the Elche players' fury with the decision appeared justified.
However, as the match ticked into its 96th minute, Ronaldo calmly struck the ball into the corner for his sixth goal in six matches this term.
"Anyone who watched the game can decide why Madrid won, it's very clear," Elche captain Jose Albacar said.
"We are going away with the feeling that a match has been stolen from us that we could have drawn against a very good team," he added.
Real's late winner was desperately hard on Elche, who created the clearer chances in the first half and refused to throw in the towel even after going behind against a club which dwarfs them in wealth and prestige.
Their entire squad is worth less than half the world record €100 million (£86m) Real paid for Wales winger Gareth Bale this month and their budget of 6 million last season is dwarfed by Real's of more than half a billion.
Post-match coverage was dominated by the late penalty decision and Barca's Spain centre back Gerard Pique joined the debate by posting an ironic comment on Twitter in which he referred to the match as a "comedy film".
Pique's Tweet prompted a scathing response from Real coach Carlo Ancelotti, who told a news conference the Spain international should "focus on playing rather than talking".
The victory put Real on 16 points, two behind Barca and Atletico, who are the only teams with a perfect six wins in six games this season.
Valencia's troubled start continued when they again played poorly and had to rely on a scuffed Jonas effort in the fourth minute of added time to secure a 1-0 win at Granada in the earlier kickoff.
Valencia's preparations for the match in Andalucia were disrupted after their France defender Adil Rami criticised coach Miroslav Djukic in a radio interview, labelled his team mates "sycophants" and was sent home.
Djukic's players turned in an error-strewn and toothless performance against a Granada side that flirted with relegation last season, barely mustering a shot on target until substitute Sergio Canales sprinted clear on the left and crossed for Jonas to bundle the ball into the net.
Valencia are seventh on nine points from six matches and Wednesday's performance will do little to ease the pressure on Serb Djukic, a former Valencia player who took over from Ernesto Valverde at the end of last season.
Valencia's fellow strugglers Sevilla secured their first win of the campaign with a 4-1 success at home to Rayo Vallecano, who had two players sent off at the Sanchez Pizjuan.
Unai Emery, a former Valencia coach, has also been under pressure after Sevilla recorded three defeats and two draws in their opening five matches.
Ivan Rakitic and Carlos Bacca both scored twice to help lift Emery's team off the bottom of the table.
- Sports & Recreation