Senegal registered a fourth win in four games in Group L after Sadio Mané set up Boulaye Dia's 18th-minute goal in a 1-0 victory over Mozambique, giving the Senegalese a chance to defend their title next year in Ivory Coast.
Mané missed an open goal with a header with about a minute to play, a chance to extend his record as his country's leading scorer, but it didn't matter as Senegal held on for the win.
South Africa's chances hung by a thread after it blew a 2-0 lead at home against Liberia to draw 2-2 last week, but Bafana Bafana won Tuesday's return game 2-1 in Monrovia. Midfielder Mihlali Mayambela sent South Africa to the African Cup in just his fourth appearance for his country when he forced a loose ball in from close range early in the second half to break a 1-1 deadlock.
That led to South Africa coach Hugo Broos celebrating joyously on the sidelines at the end, a stark contrast to his gloom four days ago when South Africa threw away its lead at home against Liberia and Broos stormed off the field at the final whistle.
Morocco had already qualified from Group K, with South Africa clinching the other place in the group. Algeria has also already qualified for the 24-team African Cup.
Egypt is close to joining them after Salah scored again in a 4-0 win in Malawi to keep his team top of Group D and needing just one draw from its last two qualifiers; away against Guinea in June and at home to Ethiopia in September.
Salah, who scored the opener in a 2-0 home win over Malawi last week, scored the second goal this time and Tarek Hamed, Omar Marmoush and Ahmed Sayed were also on target.
Namibia produced the big surprise of the day by beating five-time African champions Cameroon 2-1, a result that puts Namibia top of Group C, a point ahead of Cameroon.
The African Cup was supposed to be played in June and July this year but was postponed to January-February 2024 by the Confederation of African Football because of fears that Ivory Coast's midyear rainy season would spoil the tournament.
The African Cup returning to its January-February slot is likely to cause more friction with top European clubs, who will again lose their best African players midway through their seasons.
The last African Cup in Cameroon in 2022 was also meant to be played in June-July to avoid a clash with the European season but was also moved to the start of the year because of Cameroon's monsoon rains.