Karim Benzema doubled his Champions League tally this season with a goal in either half as Real overtook their opponents and moved into pole position.
However, Pierre-Emerick Aubameyang continued his remarkable scoring sequence with his 19th goal in as many games on the hour and substitute Marco Reus struck two minutes from time to wrest back top spot and with it a new Champions League record.
Reus' goal was the 21st scored by Dortmund in the Champions League group stage, surpassing the mark set by Manchester United (1998-99), Barcelona (2011-12 and this season) and Real themselves (2013-14).
Dortmund arrived in Madrid needing only a draw to retain first place in their final group match but found themselves under pressure almost from the off once Andre Schurrle had missed the target with an early chance.
Gonzalo Higuain's stunning strike saw Juventus finish on top of Champions League Group H as they beat Dinamo Zagreb 2-0 in Turin.
After a lifeless first half, Higuain fired Juve ahead shortly after the break before Daniele Rugani headed in a second to wrap up the victory and confirm the Bianconeri's place as group winners.
Juve were already through to the knockout stages of the competition but needed a win or for second-placed Sevilla not to beat Lyon in order to top the group.
Lyon and Sevilla played out a 0-0 draw in France while Dinamo could finish no higher than bottom of the group.
Woeful Leicester lost their unbeaten Champions League record as a second string side suffered a humiliating 5-0 thumping in Porto.
The much-changed Foxes had already qualified from Group G, while Porto reached the last 16 with victory, but barely put up a fight as they were outclassed.
Andre Silva, Jesus Corona and Yacine Brahimi scored in the first half before Silva added a second-half penalty and Diogo Jota netted a fifth.
Tottenham rounded off a frustrating Champions League campaign with a 3-1 victory over CSKA Moscow that secured the dubious consolation of a place in the Europa League.
Mauricio Pochettino's side were already eliminated from the Champions League before Wednesday's final Group E fixture, but they finished on a high note with their first victory at Wembley at the third attempt.
After falling behind to Alan Dzagoev's strike, Tottenham equalised through Deli Alli and Harry Kane netted in first half stoppage-time before goalkeeper Igor Akinfeev's own goal sealed the points.