Porto won 1-0 at home last month but Malaga's 20-year-old forward Francisco "Isco" Alarcon levelled the series in the 43rd minute by scoring with a high, curling shot that beat goalkeeper Helton and went into the far top corner.
Roque Santa Cruz then got the decisive second when he rose to head a corner into the far post in the 77th, with fans in the La Rosaleda Stadium chanting "Yes we can."
"This was unthinkable not long ago and it's incredible," Isco said. "This is a dream come true for everyone here, given that no one in Malaga has lived through anything like this, but we've deserve it."
In Wednesday's other game, Bayern Munich lost 2-0 at home to Arsenal but still went through on away goals after winning the first leg 3-1 in London.
Porto played a physical game from the start, challenging hard for the ball in an attempt to protect its advantage, but the tactic quickly backfired. The Portuguese team earned three yellow cards in the first 30 minutes, and forward Steven Defour was then sent off in the 49th for his second booking after a late challenge on Joaquin Sanchez.
Isco's goal made him just the sixth Spain under-21 player to score in the knockout stages of the Champions League and he follows in the footsteps of Raul Gonzales, Jose Reyes, Cesc Fabregas, Bojan Krkic and Cristian Tello.
Porto had come close to equalizing in the 75th when James Rodríguez floated a precisely aimed free kick toward Jackson Martinez, who deflected it just wide.
Two-time European champion Porto, which hadn't lost an away game in 2013 and is currently second in the Portuguese league, two points behind Benfica, had been aiming for its seventh quarterfinal appearance.
Instead, Malaga joined Barcelona and Real Madrid in the last eight, showing the strength of Spanish football once again.
Malaga had the support of actor Antonio Banderas, who was born in the city and was in the stands wearing a team scarf.
"I came to this stadium for the first time in 1968, when I was 8 years old, and sincerely this is probably the most important match in the history of our team," Banderas said.