Moise Adilehou put Benin ahead early in the second half before Youssef En-Nesyri equalised, but Hakim Ziyech blew the chance to seal victory for Morocco by striking the post with a 96th-minute penalty.
Sofiane Boufal and En-Nesyri both missed in the shootout as Benin, who reached the last 16 on the back of three draws, triumphed 4-1 on spot-kicks.