The victory in Antananarivo followed a 1-0 away win four days ago in the first leg of the preliminary tie and gave the Madagascans a 4-2 aggregate triumph.
Madagascar last won two qualifiers in a row when they defeated Comoros 6-2 and 4-0 in a combined 2010 World Cup/Cup of Nations eliminator.
The Indian Ocean islanders join Senegal, Equatorial Guinea and Sudan in Group A of 2019 qualifying with their first fixture set to take place in Khartoum in June.
North African-based Madagascans scored the goals that gave their home fans a rare chance to celebrate an important victory.
Egypt-based Paulin Voavy (pictured) bagged a brace within 17 minutes of the kick-off and Carolus Andriamahitsinoro from Algerian club USMA scored on 80 minutes.
Ferreria Soares and Adilson Varela scored for Sao Tome e Principe.
On Tuesday, Mauritius and South Sudan have home advantage as they seek to overcome two-goal deficits against the Comoros and Djibouti respectively.