In-form Marco Asensio headed Real in front after 11 minutes but Aissa Mandi nodded an equaliser in the 33rd before the visitors’ Nacho Fernandez diverted a fumbled Keylor Navas save into his own net to give Betis the lead four minutes later.
The Benito Villamarin stadium was rocking as the hosts sensed an upset but Real captain Ramos punctured the mood with a towering header from a corner to make it 2-2 five minutes into the second half.
Gareth Bale then spurned two good chances and it was left to hot-shot midfielder Asensio to put Real back in front with a strike across goal after a blistering counter-attack.
Ronaldo then looked to have sealed the win for Real with a net-busting strike from a tight angle in the 65th, but substitute Sergio Leon pulled a goal back for Betis in the 85th minute to set up a tense finish.
Los Blancos then made sure of the points as substitute Karim Benzema pounced in added time for Madrid’s 13th goal in three games.
The victory leaves Real on 45 points from 23 games, 17 behind runaway leaders Barcelona, although they can climb above Valencia into third place if they win their game in hand at Leganes on Wednesday.