Kylian Mbappe had a goal disallowed in the first half for an offside, and Lewandowski then scored twice in the early stages of the second period to take his tally for the campaign to 17 goals in 14 appearances, before Yamal added a late third for the visitors to become the youngest player to register in El Clasico at the age of 17 years and 106 days old.
Raphinha also got in on the act late on, lifting the ball over onrushing goalkeeper Andriy Lunin to cap a quite extraordinary evening of football in the Spanish capital.
Barcelona have opened up a six-point lead over second-placed Real Madrid at the top of the La Liga table, while the Catalan side have inflicted a first league defeat on their rivals since September 2023, with the capital team's long unbeaten run now over.
The first half-chance of the match came for Barcelona in the 13th minute, with Yamal finding himself in a dangerous area, but the Spain international's effort was kept out by Real Madrid goalkeeper Lunin.
Vinicius Junior was Real Madrid's biggest threat, and he had the chance to make the breakthrough in the 23rd minute after a solo run, but the Ballon d'Or favourite pulled his effort wide of the near post.
Mbappe thought that he had given Los Blancos the lead on the 30-minute mark with an excellent finish, but following a VAR review, the goal was disallowed, with the Frenchman just past the final defender when the ball was played.
Real Madrid were incredibly caught offside on eight occasions in the first half, with Barcelona's high defensive line operating extremely well, and the Catalan giants then scored two quick-fire goals early in the second period.
Both came from Lewandowski, with the striker making it 1-0 with a cool finish after breaking onto a pass from Marc Casado, before doubling the away side's advantage less then two minutes later by heading a cross from Alejandro Balde into the back of the net.
Lewandowski hit the post in the 66th minute, but Barcelona did make it 3-0 in the 77th minute, with Yamal collecting a pass from Raphinha before smashing the ball past Lunin between the Real Madrid sticks to create history.
Raphinha then made it 4-0 with a clever finish in the 84th minute, with Barcelona moving six points clear of their rivals at the top of the table courtesy of an incredible victory in the Spanish capital.
REAL MADRID VS. BARCELONA HIGHLIGHTS
Kylian Mbappe disallowed goal vs. Barcelona (30th min, Real Madrid 0-0 Barcelona)
Mbappe celebrates after brilliantly chipping the ball over the onrushing Inaki Pena, but the goal is disallowed following a VAR check. It was a wonderful finish from the France international, but Barcelona survive.
The 25-year-old is denied a first goal in El Clasico.
Robert Lewandowski goal vs. Real Madrid (54th min, Real Madrid 0-1 Barcelona)
Barcelona make the breakthrough in El Clasico, and it comes from the in-form Lewandowski, with the experienced striker breaking onto a pass from Casado before expertly picking out the bottom corner of the goal.
Robert Lewandowski goal vs. Real Madrid (56th min, Real Madrid 0-2 Barcelona)
Lewandowski scores again! The Poland international has his second in the space of two minutes here, as he heads a cross from Balde into the back of the net; Real Madrid have been stunned at the start of the second period.
Lamine Yamal goal vs. Real Madrid (77th min, Real Madrid 0-3 Barcelona)
Just incredible - Barcelona have a third in El Clasico, and it is 17-year-old Yamal, with the forward collecting a pass from Raphinha before smashing the ball into the back of the net. This has been some performance from Barcelona, and Yamal has just become the youngest player to ever score in El Clasico.
Raphinha goal vs. Real Madrid (84th min, Real Madrid 0-4 Barcelona)
Barcelona make it 4-0 at Bernabeu, with Raphinha breaking onto a pass from Inigo Martinez before producing a wonderful finish. Real Madrid's first league defeat since September 2023 is an incredible one.
MAN OF THE MATCH - ROBERT LEWANDOWSKI
Lewandowski was simply unstoppable in El Clasico, producing two wonderful finishes to send Barcelona two goals ahead early in the second period, and he had opportunities to add more in the period that followed.
The Poland international has already scored 17 times this season, and he was the best player on the pitch in the latest instalment of El Clasico, overshadowing the likes of Vinicius, Mbappe and Jude Bellingham, but his teammates Yamal and Raphinha ran him close.
REAL MADRID VS. BARCELONA MATCH STATS
Possession: Real Madrid 42%-58% Barcelona
Shots: Real Madrid 9-15 Barcelona
Shots on target: Real Madrid 4-7 Barcelona
Corners: Real Madrid 10-3 Barcelona
Fouls: Real Madrid 15-17 Barcelona
BEST STATS
WHAT NEXT?
Barcelona will be aiming to make it six straight wins in all competitions when they continue their La Liga campaign against Espanyol next Sunday, with the two teams locking horns in the Catalan derby.
Real Madrid, meanwhile, will be looking to bounce back from their first league defeat since September 2023 when they head to struggling Valencia next Saturday.