Guinea, Mauritania and the Ivory Coast also sealed their places.
Mahrez scored twice in the opening half hour as Algeria raced into a three-goal lead away against Togo, before eventually winning 4-1 to seal top place in Group D.
Mauritania qualified for the first time from Group I, as they came from a goal down to beat Botswana 2-1 in Nouakchott.
Guinea qualified without kicking a ball several hours before their Group H match against the Ivory Coast in Conakry, when Rwanda and the Central African Republic drew 2-2 in Kigali.
Guinea then went on to win the group with a 1-1 home draw against the Ivorians, who got the point they needed for qualification.
Burkina Faso, Tanzania and Zimbabwe all missed out on a chance to book their berths. Burkina Faso were beaten 2-1 in Angola, while Liberia defeated Zimbabwe 1-0 in Monrovia. Tanzania lost 1-0 away in Lesotho.
The top two teams in the 12 groups qualify except Group B which includes already-qualified tournament hosts Cameroon, where Morocco have made sure of the single slot.
Egypt, Madagascar, Senegal and Tunisia all booked their place last month while Mali, Morocco, Nigeria and Uganda were added to the list on Saturday.
The rest of the 24-team line-up will be decided in the final round of qualifying in March.
KINDLY DROP A COMMENT BELOW