LIBREVILLE, Gabon -- Mali set up a quarterfinal clash with co-host Gabon after coming from behind to win 2-1 against Botswana in the African Cup of Nations on Wednesday.
Mogakolodi Ngele gave Botswana the lead in the 50th minute, but Garra Dembele equalized in the 56th and Seydou Keita curled home the winner in the 74th to secure second place in Group D for Mali.
Guinea drew 1-1 with group winner Ghana in the other match to finish behind Mali in third place. Ghana will play Tunisia in the quarterfinals.
Botswana, needing a freak set of results to qualify, made Mali sweat after Ngele's opener had briefly pushed Guinea into the second qualifying berth.
Mali's players erupted in celebration when the result between Ghana and Guinea came through.
Both Mali and Botswana were coming off defeats in the previous round of matches, with Botswana's 6-1 collapse against Guinea prompting four changes to the starting lineup, including veteran striker Dipsy Selolwane dropping to the bench.
Mali made three changes to the team that lost 2-0 against Ghana last time out, with Dembele, goalkeeper Oumar Sissoko and midfielder Samba Sow all starting.
Mali had an early chance when Modibo Maiga drove into the box and lifted a cross into the middle, but Dembele failed to react quickly enough and headed over in the seventh minute.
A backheel from Botswana defender Ndiyapo Letsholathebe released Boitumelo Mafoko in the box in the 21st, but his control was poor and the shot skewed wide.
After a cagey first half, Botswana started the second period much more positively and took the lead when Moemedi Moatlhaping put a low cross into the box for Ngele to sidefoot home with his left foot.
The lead lasted just six minutes as Mali, suddenly staring at elimination, was stung into action. Abdou Traore teed up a cross to the back post where Maiga's header forced a parry from goalkeeper Modiri Marumo, only for Dembele to score from the loose ball.
As the players wheeled away to celebrate, Mali coach Alain Giresse gave a thumbs-up signal as his team again moved into the qualification positions.
Mali almost made the game safe when Ousmane Coulibaly beat three defenders to burst into the box, but prodded his shot against the bar in the 69th.
Seydou Keita then gave Mali breathing space in the match after bending a first-time shot past Marumo and into the top corner.