Los Blancos entered the match knowing that their position at the summit would be secured if they avoided defeat to Napoli, and Carlo Ancelotti's side were on the right end of a six-goal thriller in the Spanish capital.
Real Madrid have claimed their spot as Group C winners ahead of their final clash with Union Berlin, while Napoli remain second on seven points, three points ahead of third-placed Braga, meaning that the Serie A outfit are in a very strong position to also advance to the round of 16.
The hosts came close to making the breakthrough inside the first two minutes, but Rodrygo's strike was saved by Napoli goalkeeper Alex Meret.
It was Napoli that struck first at Bernabeu, with Giovanni Simeone poking a low cross from Giovanni Di Lorenzo into the back of the net from close range to silence the home supporters.
The response from Real Madrid was hugely impressive, though, with Rodrygo levelling the scores in the 11th minute; the Brazilian worked some space inside the Napoli box after a powerful run by Brahim Diaz before curling the ball into the top corner.
Jude Bellingham then sent Ancelotti's side ahead in the 22nd minute with his 15th goal of the campaign, with the England international heading a searching cross from David Alaba into the back of the net from inside the box.
Real Madrid goalkeeper Andriy Lunin was called into action down the other end in the 24th minute to keep out a powerful strike from Simeone, with Napoli continuing to look dangerous in the final third of the field.
Brahim sent one just wide of the Napoli goal in the 30th minute after another breakaway involving Rodrygo, who was causing problems with his pace and dribbling skills.
The visitors came close to a second late in the first period courtesy of a deflected effort from Piotr Zielinski, but the ball just looped over the crossbar, before the lively Brahim missed the target with a strike down the other end, with the score staying 2-1 to the hosts at the interval.
Napoli scored again early in the second period, though, with Frank Anguissa finding the back of the net with a powerful strike from a narrow angle after his initial effort was blocked by Dani Ceballos.
Real Madrid came close to regaining the lead just before the hour through Joselu, who had only just entered the field, but his header from a Toni Kroos corner just missed the crossbar.
Joselu then passed up an even better chance in the 62nd minute, with the forward somehow missing the ball from close range after attempting to meet a cross from Ferland Mendy.
Victor Osimhen had the ball in the back of the Real Madrid net in the 68th minute, but the offside flag was raised, with the two teams remaining level at 2-2 heading towards the final 20 minutes.
Real Madrid twice came close 10 minutes from time, but Meret kept out a header from Antonio Rudiger, before Joselu's acrobatic attempt missed the target after smart work from Rodrygo.
Joselu again nodded wide in the 80th minute, but the home side scored a third in the 84th minute, and it was a special moment for Nico Paz, with the youngster scoring his first senior goal when his effort from distance proved to be too powerful for the Napoli goalkeeper.
Real Madrid lost Rodrygo to another injury in the latter stages, and the club will now be sweating over his fitness ahead of Saturday's La Liga clash with Granada.
Ancelotti's side managed to secure all three points in the 94th minute, though, with Joselu finally getting his name on the scoresheet when he turned a low Bellingham cross into the back of the net.