The result advanced the Comoros to nine points from five matches, guaranteeing them a top-two finish in Group G, a fairytale achievement for the small island nation.
Assan Ceesay scored around the hour mark for the Gambia as they beat Angola 1-0 at home and they will be joined in the finals from Group D by Gabon, who hammered Democratic Republic of Congo 3-0 with Pierre-Emerick Aubameyang among their scorers.
Egypt also qualified for a record 25th appearance as Mohamed Salah returned to their team for the first time since the 2019 finals and they drew 1-1 with 10-man Kenya in Nairobi.
Ghana will participate in their 23rd tournament after a 1-1 draw with South Africa in Johannesburg ensured they will finish in the top two in Group C. South Africa and Sudan will battle it out for the other place in Khartoum on Sunday.
Zimbabwe had to wait, after winning 1-0 in Botswana, to see whether they would qualify. Neighbours Zambia needed to beat Algeria to keep their hopes alive but drew a thrilling game 3-3. Patrick Daka scored twice for Zambia and Islam Slimani got a double for the defending champions.
Zimbabwe have now qualified for a third successive finals.
Although, with two games left to complete their programme, Kenya could still get nine points, the Comoros have a better head-to-head record in the two matches between the two countries.
Togo, who had already been eliminated and left almost all foreign-based regulars behind for the trip to the Comoros, still had the better of limited opportunities in a drab encounter.
The Indian Ocean island archipelago, with a population under 900,000, have only been competing in international football since 2006 but have become more competitive in recent years after drawing more players into the team from the Comorian community in France.
Their 1-0 win over Togo in November 2019 at the start of the Cup of Nations qualifiers was a first-ever away success for the Comoros in a competitive international and they held Egypt to a draw at home four days later.
"The pride I feel for my country is indescribable! Bravo to my players, my staff, my federation, thank you to the President. And a big thank you to our incredible supporters. Together we made history. See you at the Cup of Nations!," coach Amir Abdou said on Twitter.