Substitute Hameur Bouazza scored the decisive goal two minutes into extra-time as Algeria dumped Côte d'Ivoire out of the Africa Cup of Nations.
In what was an impressive display by Rabah Saadane's team, they were twice forced to come from behind having trailed to a Salomon Kalou strike and then a Kader Keita effort at the death.
In between, Karim Matmour scored an equaliser just before half-time, before a Madjid Bougherra header took the tie into extra-time.
But Blackpool forward Bouzza netted a dramatic winner two minutes after coming on to hand his side a well-deserved win against Vahid Halilhodzic's below par Elephants.
It was a dramatic turnaround after the Algerians looked nervous through the opening exchanges and fell behind four minutes in.
Yaya Toure's shot was deflected into the path of Kalou on the right side of the penalty area and the Chelsea striker executed a low finish beyond Faouzi Chaouchi at the near post.
It continued to look easy for the Elephants as Didier Drogba had a penalty appeal turned down - the referee deemed Rafik Halliche's sliding challenge a fair one.
The Algerians eventually began to settle as the game reached the 20-minute mark and Boubacar Barry almost gifted them a goal when his attempt to make a routine clearance was charged down by Abdelkader Ghezzal only for the ball to rebound wide.
More Algeria pressure followed as the momentum looked to have totally shifted and, apart from occasional tussles between Drogba and Bougherra, the Algerians took charge.
The pressure eventually told five minutes before half-time when a long ball from Antar Yahia bounced loosely on the edge of the Ivorian penalty area, with Matmour forcing his way in between two defenders and finishing emphatically with a strike that took the upright before going in.
That brought up half-time and after a quiet restart, the pace dramatically picked up as the game approached the hour mark.
First Matmour missed a glorious chance to put the Desert Hawks in front when he blazed over from just inside the box after receiving a delightful pass over the top from Mourad Meghni.
There was a crucial moment in the 68th minute when Souleymane Bamba was dispossessed by Matmour on the edge of the penalty area, but the Borussia Monchengladbach midfielder saw his shot excellently kept out by Barry.
Saadane's men continued to pile on the pressure, but their west African opponents were still a threat on the break as Yaya Toure illustrated with another shot that forced a full length dive from Chaouchi, but drifted narrowly wide.
Five minutes from the end and having just come on, substitute Kader Keita led another sweeping counter attack with a pin-point pass to Gervinho, but the Lille forward blasted his effort over.
The Galatasaray midfielder then smashed in a 25-yard screamer that went in off the upright and looked to have won the game with just a minute to play.
Enter stoppage time and the Algerians refused to give in.
A sweeping cross from the left, along with sloppy defending from the Ivorian defence, allowed Bougherra in and the Rangers defender headed in powerfully.
The Elephants looked shell-shocked and their opponents took full advantage less than two minutes into extra-time.
Bouazza, introduced after the 90 minutes ended, was left unmarked at the far post and leapt high to nod in Ziani's cross from the left to put Algeria ahead for the first time.
Their opponents tried to respond with Drogba's fierce 18-yard shot somehow blocked by Chaouchi, who helped his side stay 3-2 ahead after 105 minutes.
Straight from the restart there were chances aplenty for the Algerians to kill off the game as Côte d'Ivoire went for broke - Ghezzal mis-hitting the best of the chances in the form of a virtual tap-in that seemed almost impossible to miss.
At the other end, Bamba put a goal-mouth scramble for his side wide, while the final attempt, which came to Kolo Toure and was expertly executed into the back of the goal, was judged offside as the tournament favourites bowed out.