Barcelona overcame the shock of conceding a goal after just 23 seconds - the fastest in El Clasico history - to defeat Madrid 3-1.
French striker Karim Benzema scored the early goal to give Real a 1-0 lead after a terrible clearance from Barcelona goalkeeper Victor Valdes.
Messi gave Real reason not to get carried away with a trademark run, going round Sergio Ramos, and seeing his strike parried by Casillas.
The home side should have doubled their lead midway through the half when Ronaldo fired wide inside the box with time to pick his spot.
Alexis Sanchez, who was chosen ahead of David Villa, levelled in the 30th minute after a sublime through ball from Lionel Messi.
A drive from Xavi in the 53rd minute took a wicked deflection off Marcelo and beyond the desperate reach of Iker Casillas in the Real goal as Barcelona went into a 2-1 lead.
Real had the chance to equalise through a Ronaldo header which he failed to keep on target from just 10 yards out, but Barca were also a danger at the other end.
Cesc Fabregas then added the third in the 65th minute, scoring with a diving header from a fine cross by Dani Alves.
Real, who now trail Barca on goal difference, still have a game in hand, but this result brought to an end a 15-match winning streak for Jose Mourinho's side.