Zambia defeated African Cup of Nations co-hosts Equatorial Guinea to qualify ahead of them as Group A winners, with captain Christopher Katongo's second-half strike the difference between the sides.
Equatorial Guinea were already assured of their place in the quarter-finals after winning their first two matches, but lost out on the chance of a favourable draw as they failed to get to grips with Herve Renard's side at the Estadio de Nuevo.
The result also meant that despite their win Libya failed to qualify for the knock-out stage of the competition. Marcos Paqueta's men needed to win their game and hope Equatorial Guinea would beat Zambia and, although Al Busaifi Ihaab's double in the 2-1 win over Senegal ensured they kept their side of the bargain, Zambia's win ensured it was nothing more than a proud farewell for Libya.
They and Senegal - who equalised on the night through Deme N'Diaye - are both now out, with much-fancied Senegal ending without picking up a single point.
Knowing that they needed to first and foremost take care of their own result, Libya started well and got off to the perfect start, opening the scoring in the fifth minute.
A jinking run from Admed Saad created some space and, after he played a reverse pass, Ihaab was on hand to do the rest from just inside the area.
The lead was shortlived, though, with N'Diaye getting ahead of his front-post marker to nod home Mamadou Niang's ninth-minute corner.
Niang himself then blazed over from distance, with Jamal Mohamed doing the same at the other end, before Senegal's Souleymane Camara saw his close-range hit brilliantly kept out by Samir Abod.
Camara was making a real nuisance of himself and his cross then found Pape Malickou Diakhate, but his header was well saved by Abod.
That was the last meaningful action of the first half and the last real incident until the hour mark, which saw Ahmed Zuway's strike blocked by Kader Mangane as Libya hunted a second.
That could have come when Mohamed El Moghrabi's corner found Abdalah Al Sherif, only for him to miss the target, before Mangane got in the way of another Zuway effort.
Newcastle-bound Papiss Cisse then swung a shot over the bar, before Ali Samala went close to scoring for Libya; his header being kept out by Khadim N'Diaye.
A second Libyan goal did arrive, though, and when it did, with six minutes remaining, it was a memorable one.
Zuway cushioned a long ball into the path of the onrushing Ihaab and he spectacularly volleyed it into the bottom corner.
Cisse tried his best to get Senegal level for a second time just after, but time was not on their side as Libya held on.
In the Group's other match, Zambia had the better of the chances but a stalemate looked likely until Katongo collected a throw-in on the left, dribbled the ball into the penalty area and beat Clementino Emmanuel Danilo at his near post with 67 minutes gone.
Katongo tried his luck early on with a 30-yard strike in the third minute, but it did not require Danilo to make the save.
Zambia continued to press and Davis Nkausu found Emmanuel Mayuka on the six-yard line, though he somehow headed over.
Equatorial Guinea were short of the attacking prowess they had shown in their previous matches and were stuggling to muster any serious attempts.
Zambia's next opportunity came after 35 minutes when Rainford Kalaba jinked right and left on the edge of the area before laying the ball to Katongo, who took a touch past Daniel Evuy before seeing his shot saved smartly by Danilo.
With five minutes left in the half Ivan Bolado registered the home side's first proper shot on target, but it did not worry Kennedy Mweene.
Just after the restart Zambia were lucky to win an indirect free-kick in the area. Kalaba teed up Hichani Himonde but his powerful shot was bravely charged down.
Zambia were in control but lacked finesse in the final third, with Kalaba and Stophira Sunzu shooting wastefully.
But with 23 minutes to play, Katongo produced his moment of quality, taking the ball from Joseph Musonda and driving into the danger area before finally defeating Danilo.
A response did not seem likely and, one weak 25-yard strike from Viera Ellong aside, the local fans were given little to cheer.
Today the Ivory Coast, who are already ensured of qualification from Group B, play Angola, who must win to qualify, and the other game in the group sees Sudan, who must win and hope Ivory Cioast win to qualify, play Burkina Faso, who have no chance to qualify, but a win would see them finish the group third.