Mbappé threatened to haul Madrid back into the title race, but Raphinha's double and goals from Lamine Yamal and Eric Garcia gave Barcelona a seven-point lead on the reigning champions with three matches remaining.
Hansi Flick's side were knocked out of the Champions League in the semi-finals by Inter Milan on Tuesday and risked their season collapsing if they were beaten by Madrid, but they resisted after Mbappe's early brace.
Barcelona battled back to claim a fourth Clasico victory from four meetings this season, showing their dominance over Carlo Ancelotti's side.
"We've opened up an important gap, it was key to win this game after the Champions League (defeat), the fans have forgotten it, and so have we, let's enjoy this," Yamal told 'Movistar'. "It was important to win today to bring the league title closer, and it went very well, we're very happy".