Olympique de Marseille forward Khaoui netted his first international goals either side of the break following the opener from Khazri, who knocked in the rebound in the 33rd minute after his penalty was saved and struck again late on.
Hamdou Elhouni got Libya's consolation just before halftime.
In the other Group J match, Tanzania came from behind to defeat Equatorial Guinea 2-1 in Dar es Salaam.
Salum Abubakar got a spectacular winner with a scorching drive from outside the box in added time.
Pedro Obiang had given the visitors a first-half lead as his speculative shot somehow beat Tanzania goalkeeper Juma Kaseja, but Simon Msuva levelled for the hosts in the 69th as he turned home the rebound after his initial header struck the post.
Morocco's indifferent start under new coach Vahid Halilhodzic continued when they were held to a 0-0 draw by Mauritania in their Group E encounter in Rabat.
Zimbabwe also played out a goalless draw with Botswana in their Group H clash in Harare in which the hosts made all the chances but had little firepower in front of goal.