Ibrahim Amada's wonder strike put Madagascar ahead just nine minutes in at Alexandria Stadium but DR Congo responded swiftly as China-based forward Cedric Bakambu headed home his third goal in two matches. Madagascar captain Faneva Andriatsima nodded the Indian Ocean islanders back ahead with a quarter-hour remaining only for Chancel Mbemba to haul DR Congo level at a corner in the final minute.
Marcel Tisserand and Yannick Bolasie both blazed over for DR Congo in the shootout as Madagascar prevailed 4-2 with their reward a meeting with four-time winners Ghana or 2004 champions Tunisia for a place in the semi-finals.
"I want to congratulate my players, particularly after we had to deal with that equaliser right in the last minute of normal time," said Madagascar coach Nicolas Dupuis. "We weren't quite as good as we were against Nigeria. We lacked a bit of juice but our opponents created us a lot of problems for us." He added: "I think 25 million will be partying at home. We want to go as far as possible and enjoy it."
Marco Ilaimaharitra's suspension forced Dupuis to alter his line-up for the first time in the tournament with Rayan Raveloson deputising in midfield for a team that shocked Nigeria 2-0 to advance as group winners.
Britt Assombalonga replaced the injured Jonathan Bolingi in the lone change to the DR Congo side that trounced Zimbabwe 4-0, with captain Youssouf Mulumbu describing their qualification as a "miracle" following two opening losses.
Madagascan President Andry Rajoelina chartered a 480-seater plane to take fans to Egypt, and the travelling support were soon rejoicing when Amada blasted a sensational 25-yard strike that tailed away from Ley Matampi and flew in off the post.
The jubilant celebrations were halted midway through the first half though when Bakambu slipped free of the Madagascar centre-backs to glance in Glody Ngonda's bending cross.
Mbemba smashed over as another left-wing cross found the Malagasy defence in trouble, but this was a far more balanced contest than the last meeting -- a 6-1 triumph for DR Congo in a 2017 Cup of Nations qualifier.
That hiding was the darkest day in Malagasy football history but they have undergone a dramatic transformation, and nearly regained the lead 10 minutes after the break when Jerome Mombris embarked on a driving run, his shot spilled by Matampi with Charles Andriamahitsinoro agonisingly hitting the rebound wide.
A battling Madagascar eventually regained the lead against a nation ranked 59 places above them when Andriatsima flung himself at a Romain Metanire cross following a superb long-busting run from the right-back.
Bolasie's tumble in the area provoked furious protests from DR Congo, with VAR not in use until the next round, but the Leopards conjured up another escape act in the 90th minute as an unmarked Mbemba headed in a corner to force extra time.
Madagascar goalkeeper Melvin Adrien kept his team level in extra time with a string of vital saves, the best an instinctive one-handed stop to repel another Mbemba header, while some desperate defending allowed the 'Barea' to take it to a shootout and add a further chapter to their Egyptian fairytale.
Standout matches today