Javier Balboa's controversial second-half penalty put hosts Equatorial Guinea on course for a 2-0 victory that sent them through to the quarter-finals of the Africa Cup of Nations.
Esteban Becker's men knew only a win at Estadio de Bata on Sunday would be enough to secure their progression from Group A, and Balboa's spot-kick put them in front before a late second goal from Iban Salvador ensured they advanced to the last eight of the tournament for only the second time.
Gabon will feel hard done by with the opening goal, as referee Noumandiez Doue harshly adjudged Lloyd Palun to have upended Balboa.
Equatorial Guinea will not care about that, though, after securing their first win of the competition to qualify as runners-up behind Congo.
Jorge Costa's Gabon only needed a point from the final group game to go through, but bow out after following their opening win over Burkina Faso with successive defeats.
Raul Fabiani was preferred to Iban in attack for the hosts, who had the first chance to open the scoring in a scrappy start.
That opportunity came the way of Balboa after only eight minutes, but he fired a free-kick over the crossbar after goalkeeper Didier Ovono was harshly adjudged to have handled outside the penalty area when launching the ball upfield.
Equatorial Guinea threatened again 18 minutes in, yet Bruno Ecuele Manga was alert to the danger as he turned Balboa's dangerous cross away for a corner with Fabiani behind him at the back post.
Gabon captain Pierre-Emerick Aubameyang came close to opening the scoring soon after, racing into the area to meet a Frederic Bulot cross with a downward header that forced a fine reaction save from Felipe Ovono.
Bulot then had a chance of his own as Gabon raised the tempo, but the midfielder shot too close to Felipe Ovono after playing a neat one-two with Malick Evouna.
The Equatorial Guinea keeper was called into action once more just two minutes into the second half, palming over a dipping free-kick from Borussia Dortmund striker Aubameyang.
Felipe Ovono thwarted Gabon yet again when he showed great agility to repel a left-footed free-kick from Bulot that looked destined for the top corner, and it was Equatorial Guinea who then took the lead against the run of play in the 55th minute.
Balboa went down belatedly after Palun had stuck out a leg, but referee Doue pointed to the spot and the Estoril forward picked himself up to slot home the resulting spot-kick and send the crowd into raptures.
Fabiani could have doubled the hosts' lead, only to be denied by Didier Ovono after racing away down the right, and Bulot spurned a glorious chance to equalise when he blazed wildly over the bar as the tension mounted.
Didier Ovono also denied Balboa a second, but Iban sealed the hosts' progress and sparked wild celebrations with a close-range finish four minutes from time, after Emilio Nsue's initial shot had been kept out by the Gabon keeper.
Watch every match of the Africa Cup of Nations LIVE on British Eurosport HD or sign up to Eurosport Player and follow the action on mobile, tablet and other devices: www.eurosportplayer.comcomments