The shocking victory gives the 2014 champions a narrow lead ahead of the return leg next Friday, 18 August 2017 in the Tunisian city of Sfax.
Oussama Darfalou gave the host the lead after just 60 seconds with a low shot that beat Libyan goalie Mohamed Nashnush.
The visitors responded five minutes later through Masaud who finished off a quickly taken free-kick for a deserving equalizer.
Three minutes after the break, Ellafi completed a three-man move with Mohamed Aleyat and Mohamed Ghanudi as the other actors to seal victory.
Elsewhere in Nouakchott, Mauritania were held to a 2-2 draw by visiting Mali, in a game which saw all four goals in the first half.
Mandala Konte put Mali up in the opening minute before Abdoulaye Gaye converted a penalty to level matters on nine minutes. Samba Moussa ensured Mauritania went up for the first time on the quarter-hour mark only for Aboubacar Diarra to draw even on 21 minutes.
‘Les Etalons’ of Burkina Faso came from behind twice to hold neighbours Ghana to a 2-2 draw in Ouagadougou. The Black Stars went up on 14 minutes courtesy Sadick Adams from the spot.
Mohamed Sylla levelled for the Burkinabes on 55 minutes but their joy was cut short ten minutes later, when Gideon Waja restored Ghana’s lead. Ilasse Sawadogo grabbed the equalizer six minutes from time setting the stage for crucial duel in the return leg on Sunday, 20 August 2017, in Kumasi.
11.08.2017 Brazzaville Congo 0-0 DR Congo
12.08.2017 Malabo Equatorial Guinea vs Gabon
12.08.2017 Sao Tome Sao Tome vs Cameroon
12.08.2017 Kampala Uganda 3-0 Rwanda
13.08.2017 Hawassa Ethiopia vs Sudan
12.08.2017 Constantine Algeria 1-2 Libya
13.08.2017 Alexandria Egypt vs Morocco
12.08.2017 East London South Africa 2-2 Zambia
13.08.2017 Antananarivo Madagascar vs Angola
13.08.2017 Moroni Comoros vs Namibia
West A Zone
12.08.2017 Nouakchott Mauritania 2-2 Mali
15.08.2017 Pikine Senegal vs Guinea
West B Zone
12.08.2017 Ouagadougou Burkina Faso 2-2 Ghana
13.08.2017 Cotonou Benin vs Nigeria
13.08.2017 Niamey Niger vs Cote d’Ivoire