Ramos, making a record 45th appearance in the 'Clasico' fixture, was awarded the spot-kick following a VAR review, after falling in the area when his shirt was tugged by Barca's Clement Lenglet as the pair contested a corner.
The defender showed his customary calm to score from the spot in the 63rd minute and restore the champions' lead, after Federico Valverde had opened the scoring in the fifth minute only for Barca's 17-year-old forward Ansu Fati to equalise less than three minutes later.
The victory took La Liga champions Madrid back to the top of the standings on 13 points from six games, while Barca, who have only taken one point from their last three league outings, are all the way down in 10th on seven points from five games.
The win is a huge boost for Zinedine Zidane's side, who went into the match reeling from back-to-back home defeats to promoted Cadiz and a depleted Shakhtar Donetsk.
"This win gives us three points but it also gives us confidence after a difficult week for us," said keeper Thibaut Courtois.
"People were doubting us but we didn't doubt ourselves, we were always with the manager. When you wear this badge you have to give everything in the 90 minutes, and we really put in a big effort today and deserved to get the win."
Courtois and Neto each made impressive saves in the first half, while Barca midfielder Coutinho should have given the Catalans the lead shortly before the penalty but somehow missed the target with a free header teed up by Ansu.