Equatorial Guinea will face Ghana or Guinea in their first Cup of Nations semi-final on Thursday.
The game ended with ugly scenes as both sets of officials and players clashed in the 120th minute, with security getting involved, and at the final whistle some Equatorial Guinea fans ran onto the pitch to celebrate a famous win.
Bottles were thrown at Tunisia players, with one Carthage Eagles player throwing one back.
The minnows, ranked 118th in the world, only entered the tournament in November when they replaced Morocco as hosts because of the Ebola crisis.
They had been eliminated from qualifying after fielding an ineligible player, only hired coach Esteban Becker 11 days before the tournament and gave five players debuts in a pre-tournament friendly.
However they qualified from Group A thanks to victory over Gabon in their final game to reach their second quarter-final. They lost the first - in the 2012 event they co-hosted - to Ivory Coast.
Tunisia, meanwhile, topped a group with DR Congo, Cape Verde and Zambia and had good reason to enter the game in confident mood.
It was a game of few chances, with Akaichi having a shot touched over by Felipe Ovono before he opened the scoring.
Emilio Nsue was denied a leveller with eight minutes left by Tunisia keeper Aymen Mathlouthi, setting up the incredible late drama.
Bolado dribbled into the area and appeared to stand on the edge of Maaloul's foot before falling to the floor. The referee awarded a penalty in front of a partisan home crowd to a furious reaction from the Tunisian bench.
Some fans shone lasers in the eyes of Tunisia keeper Mathlouthi, who went the wrong way as Balboa rolled it into the net.
In extra time, the momentum was with Equatorial Guinea and Balboa curled home a wonderful free-kick in the first period.
Tunisia created few chances to force penalties before the match ended on a sour note with chaos in the Bata Stadium.
DR Congo fought back from two goals down to beat neighbours Congo and reach the Africa Cup of Nations semi-finals. Ferebory Dore volleyed Congo in front and Thievy Bifouma drove in from a rebound to make it 2-0 either side of DR Congo efforts that hit the bar.
In a 16-minute spell DR Congo's Dieumerci Mbokani slotted in to give them hope, Jeremy Bokila equalised and Joel Kimuaki headed a third.
DR Congo almost equalised shortly after when Cedric Makiadi's free-kick crashed against the bar. But instead they conceded again after Jean Kasusula passed the ball out of defence straight to Dore whose shot was blocked by Kidiaba into the path of Bifouma and his effort beat the defenders on the line.
However, DR Congo refused to cave in and three minutes later they finally got the goal they had been threatening, Yannick Bolasie beat two men for pace and cut the ball back from the byeline for an unmarked Mbokani to slot home.
A minute later Bokila somehow missed the target after a knockdown from the excellent Mbokani, and then Dore was inches away from making it 3-1 to Congo as the game swung from end to end.
Back came DR Congo and Bokila finally made up for his earlier wastefulness when he fired a left-foot shot into the top corner and levelled the score.
With DR Congo now in the ascendancy they used their momentum to press Congo back and won a free-kick on the left flank. Cedric Mabwati swung in the set-piece and substitute Kimuaki glanced a header into the corner to put his side in front.
A magnificent turnaround was capped when substitute Neeskens Kebano found Mbokani, who followed up after his initial effort was saved to slot home his second and his side's fourth.
He smashed in a drive from 20 yards to set up a last eight match with Group C runners-up Algeria on Sunday.
Edgar Salli missed two good chances to equalise, hesitating when clean through and also blazing over from in the box.
Those glaring misses left Cameroon bottom of the group - their worst finish at a Nations Cup finals - and it also means lots will need to be drawn on Thursday to decide whether Mali or Guinea, who played out the fifth 1-1 stalemate of Group D, take the other quarter-final place.
Ivory Coast will be relieved to have avoided that lottery, having gone out in 1988 by that method, as they did just enough to earn the win that guaranteed them passage to the knock-out stages for the sixth successive time.
The only negative for Algeria is an injury to key man Yacine Brahimi, who was forced off after 72 minutes - they will be hopeful the midfielder will be fit for Sunday's quarter-final against the winners of Group D.
Ghana piled on the pressure after the break and substitute John Boye drove home an equaliser before Ayew struck.
With 17 minutes remaining, Ghana were on the way home as they trailed 1-0 but Boye's equaliser gave them hope and then Ayew's strike confirmed they would finish ahead of Algeria, who qualified with them after beating Senegal 2-0, on head-to-head record.
Ghana had never scored against South Africa in an Africa Cup of Nations match but they started as though they were determined to end that record.