European champions Barca were cruising after early strikes from Xavi and Cesc Fabregas but they eased off and the Basques levelled with two goals in three second-half minutes.
France striker Karim Benzema led Real to victory, scoring either side of a disputed Cristiano Ronaldo penalty, but it needed substitute Gonzalo Higuain to settle the match for Jose Mourinho's men.
Real have a perfect six points, the same as Valencia who were 1-0 winners at home to Atletico Madrid.
Barca have four points along with Sociedad and Sevilla who grabbed a 2-2 draw at 10-man Villarreal.
Roberto Soldado powered a header in off the bar for Valencia in the 52nd minute and tops the scoring charts with Ronaldo, on four goals from two games.
Villarreal had goalkeeper Diego Lopez harshly sent off after 22 minutes at the Madrigal and Alvaro Negredo saw his penalty saved but scored on the rebound to put Sevilla ahead.
Giuseppe Rossi levelled with a penalty at the other end and Marco Ruben fired Villarreal ahead in the 73rd before Alexis Ruano headed a late equaliser.
MESSI ON BENCH
Perhaps with the midweek Champions League fixture against AC Milan in mind, Barca coach Pep Guardiola left Lionel Messi, David Villa and Andres Iniesta on the bench and at first they were not missed.
Fabregas dinked a ball over the top for Xavi to score in the 10th minute and the former Arsenal player also set up Pedro a minute later, putting away the rebound when the latter's shot was blocked.
Barca lost new signing Alexis Sanchez to a thigh injury, which could keep the Chilean sidelined for up to eight weeks, and the game began to get away from them in the 59th minute.
Striker Imanol Agirretxe headed one back for the home side and almost immediately the same player intercepted a poor back pass from Villa and fired against the bar.
Antoine Griezmann followed up to head into the open net, however, and Barca were lucky not to lose Sergio Busquets who had tipped Agirretxe's original shot on to the woodwork with his hand.
Guardiola threw Messi and Iniesta into the fray but it was a despairing last-minute defensive header from Dani Alves that denied the hosts a shock winner.
"It was two defensive errors... that have proved very expensive," Barca captain Xavi told Spanish television.
NOT A PENALTY
Benzema fired Real ahead at the Bernabeu after a slick passing move in the 14th minute and after Miku had levelled for Getafe just before the break, Ronaldo tumbled on the edge of the area in the 60th.
The referee signalled a penalty which the Portuguese forward scored, though television replays showed the incident occurred outside the area.
"Being totally objective it was not a penalty," Mourinho said.
Get the best features, fun and footballing frolics straight to your inbox every week.
Thank you for signing up to Four Four Two. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.