Federico Valverde opened the scoring for Madrid in the fifth minute when he fired in a pass from Karim Benzema.
Barcelona’s Ansu Fati leveled three minutes later from an attack cued by Lionel Messi.
Barcelona was taking the match to Madrid’s area when Ramos was sent to the spot by the VAR review.
Ramos restored Madrid’s advantage in the 63rd from the spot after a brush with Barcelona’s Clement Lenglet in the area. After consulting the video screen on the touchline, referee Juan Martínez ruled that Lenglet had impeded Ramos from contesting a high ball by grabbing his shirt.
Luka Modric capped the win for Madrid in the 90th with Barcelona searching for an equalizer.