After a goalless draw in last week's first leg in Spain, Dortmund looked to be heading out of Europe when they trailed 2-1 on the night with the game at the Signal Iduna Park moving into injury time.
Marco Reus gave the Bundesliga club hope when he made it 2-2 in the 91st minute but at that point Malaga were still heading through on away goals and it took Felipe Santana's close-range finish to decide the tie in Dortmund's favour.
With Madrid leading 3-0 from last week's first leg, there was never likely to be as much drama in their quarterfinal return against Galatasaray in Istanbul, and even less so after Cristiano Ronaldo put the Spaniards ahead early on.
But three goals in 15 second-half minutes gave Galatasaray some hope before Ronaldo's second of the night in stoppage time finally ended any doubt as to who would progress.
Still unbeaten in this season's Champions League, Dortmund had plenty of reason to be confident of seeing off the challenge of tournament debutants Malaga, but there must have been a nagging concern at their inability to score an away goal at La Rosaleda last week.
And they were left with an uphill struggle to qualify when Joaquin gave the visitors a 25th-minute lead on the night, capitalising on slack defending to drive a low shot through the legs of Neven Subotic and past Roman Weidenfeller in the home goal.
'One very disappointed team'
The Signal Iduna Park's huge South Stand was momentarily silenced by that goal, with Dortmund now needing to score twice to go through.
They got one back five minutes before the interval when Robert Lewandowski ran on to a stunning Marco Reus flick and scored his sixth of the season in the competition.
But Malaga looked certain to be heading through when substitute Eliseu, from an offside position, tapped in a Julio Baptista shot in the 82nd minute.
Suddenly Dortmund needed two more goals but they got one when Reus finished from close range a minute into injury time and the stadium erupted when Santana bundled in at the very end.
The Brazilian looked to be in an offside position, but the goal stood.
"There is one very disappointed team at the moment and one lucky team," admitted Dortmund coach Juergen Klopp.
"It is unbelievable. It was our worst game in the Champions League this season but over the two games I think we are deserving winners.
"If we play like tonight we won't win the Champions League," he added. "But now we've reached our target, a semifinal."
Malaga left heartbroken
In contrast, Malaga were left heartbroken, with striker Roque Santa Cruz saying: "We were four minutes away from the semis. There is huge disappointment in the dressing room. We had the tie in our hands."
In Istanbul, Ronaldo's close-range finish from a Sami Khedira centre put Real ahead on the night and left Galatasaray needing to score five times to turn the tie around.
In a remarkable 15-minute spell in the second period, Emmanuel Eboue's thumping effort levelled the scores on the night before Wesley Sneijder's neat finish made it 2-1.
Didier Drogba, a Champions League winner with Chelsea last season, then back-flicked a Nordin Amrabat cross into the net to make it 3-1, but the Turkish giants still had too much to do, and Ronaldo's second of the night – his 50th in Europe – made the final score 3-2 on the night.
Real progress 5-3 on aggregate and coach José Mourinho can now look forward to his seventh Champions League semifinal.
"In this kind of match we had everything to lose but nothing to gain," said Mourinho.
"Now we have a semi to play, against a great team. We do not know which one yet, but they will be great. It will be hard but now is the time to enjoy ourselves and not to be nervous or afraid."
The semifinal line-up will be completed on Wednesday when Barcelona meet Paris Saint-Germain and Juventus take on Bayern Munich in the other quarterfinal second legs. – AFP