Senegal took the lead after a first-half penalty from Ismaila Sarr. Moises Caicedo then scored for Ecuador to make it 1-1 in the 67th.
At 1-1, Ecuador would have advanced from Group A and Senegal would have been eliminated.
However, a few minutes later Senegal took the lead once again as their defender and Koulibaly scored from close range after a free kick whipped from the right flank.
The Netherlands beat Qatar 2-0 in the other match to win the group. Senegal finished second, while Ecuador and Qatar were eliminated.
Senegal last advanced from the group stage at the 2002 World Cup, when the team reached the quarter-finals in its tournament debut.