Morocco were beaten 4-1 on penalties, after both sides failed to win the game in 120 minutes of football.
Moise Adilehou scored for Benin eight minutes after the start of the first half but Youssef En-Nesyri pulled level for the North Africans late.
With the game locked at 1-1, winger, Hakim Ziyech was presented a glorious chance to win it from the spot for Morocco in second half additional time.
But the Ajax Amsterdam man struck his shot of the post with the last kick in regulation time.
He was then given the granted another opportunity to atone for his big miss earlier but once again, the 26-year-old fluffed his line.
The Squirrels who had Khalid Adenon sent off in first half extra time would then go on to win the game on penalties, converting all of their four kicks as Sofiane Boufal and Youssef En-Nesyri missed their kicks.
The win took Benin to the quarter finals of the competition for the first time in their history.