The Champions League holders had been beaten 1-0 in the Russian capital earlier in their Group G campaign, yet they were already assured of going through as pool winners.
Fedor Chalov, Georgy Schennikov and Arnor Sigurdsson scored the goals for CSKA on a humiliating night for Madrid, who were subjected to whistles from the home fans at full-time.
It was a much-changed Madrid side who dominated possession in the early stages, but they found goalscoring opportunities limited, with Marco Asensio seeing a tame low strike comfortably saved by goalkeeper Igor Akinfeev.
Asensio started to prove a nuisance for the CSKA defence and, having earlier clipped the crossbar with a curling strike, saw another effort tipped over before squandering a golden opportunity to break the deadlock when he was sent through one-on-one with Akinfeev only to fire straight into the goalkeeper's clutches.
When the opener arrived in the 37th minute, it was completely against the run of play.
Sigurdsson carried the ball goalwards before offloading to Chalov on the edge of the box, the striker steadying himself before placing a shot beyond the reach of the diving Thibaut Courtois.
That lead was doubled to the bemusement of Madrid fans six minutes later, when a Mario Fernandes shot bounced off Courtois and Schennikov raced into the box to finish the job.
Madrid launched a rapid counter-attack 10 minutes into the second half, but Isco's dithering allowed Schennikov to recover from his first attempt at a tackle to block the attacker's eventual strike.
And the points were in the bag for CSKA after 73 minutes, when Sigurdsson swept home from the edge of the box to complete a most unlikely victory.
What does it mean? CSKA complete rare double
The result had no impact on Madrid's topping of Group G but it did mark the first time they have lost twice to the same team in the group stage of the Champions League since they were beaten home and away by Juventus in 2008-09.
A further point of concern for Madrid will be an ankle knock to Gareth Bale. The Welshman came on as a half-time substitute for Karim Benzema and, while he was deemed fit enough to continue after picking up the injury, he did require lengthy treatment.
Isco can't catch a break
With the exception of last week's Copa del Rey clash with Melilla, this was Isco's first start since October, with the attacker having fallen out of favour since Santiago Solari took over from Julen Lopetegui.
The Spain international did little to enhance his case for a regular spot in the starting XI on Wednesday, giving away possession too cheaply and not making the most of his chances when they arrived.
Sigurdsson sets the standard
CSKA's strength was in taking their chances. From five shots on target, they scored three goals.
Sigurdsson was central to two of those strikes, embarking on the run that would set up Chalov for the opener, before rounding off the win with a confident finish.
Key Opta Facts
- This was Madrid's heaviest home defeat in any European competition in the club's history.
- Madrid have lost a group stage match at home in the UEFA Champions League for the first time since October 2009, when they lost 2-3 to AC Milan, 28 games ago.
- Arnor Sigurdsson is the first player to both score and assist at the Bernabeu against Real Madrid in the UEFA Champions League since Pierre-Emerick Aubameyang did so for Borussia Dortmund back in December 2016.
Real Madrid host Rayo Vallecano in LaLiga on Saturday, while CSKA are not back in action until March.