Spectacular goals from Dame Ndoye and Papiss Cisse gave Senegal the lead in each half only for Salomon Kalou and Gervinho to snatch equalisers, before Didier Drogba and Max Gradel scored in the final ten minutes of a high-tempo thriller.
The teams meet again in Dakar during October and the aggregate winners of the second round fixture advance to the finals of the 16-nation tournament in South Africa.
Ndoye put the visitors ahead with a volley that flew past goalkeeper Boubacar Barry, and Kalou punished slack marking to poke the ball into the far corner from close range just before half-time.
A looping Cisse header from outside the box put the Senegalese back in the lead, but it lasted only five minutes before Gervinho burst through the middle and hammered the ball into the net off the underside of the crossbar.
Jacques Faty pushed Gervinho inside the area with ten minutes left and Drogba converted the penalty before Gradel gave the Ivorians a two-goal cushion.