Morocco looked set to coast into the last four after three goals in seven first-half minutes, along with a defensive meltdown from Nigeria, left them with what appeared to be an unassailable lead.
With Nigeria reeling, their opponents nabbed a second goal thanks to Mouhssine Iajour's low, dipping free kick from the edge of the area.
The Super Eagles' hopes of progression seemed over when El Ouadi again linked up with Moutouali, before the winger unleashed a low drive beyond Agbim.
However, the half-time break seemed to reinvigorate Nigeria and Ifeanyi Ede had pulled a goal back within four minutes of the restart.
On 55 minutes, Ede turned provider when he headed down a cross for Kabiu Ali, who rifled the ball home to give the Super Eagles hope of an unlikely comeback.
Their equaliser came in dramatic fashion when Ejike Uzoenyi picked the ball up on the edge of the area and, after beating two players, unleashed a fierce shot that hit the underside of the bar and nestled in the back of the net.
After forcing extra time, Uzoenyi continued to threaten the Morocco defence, seeing one shot saved and providing a chance for Ali, which was squandered in the first period.
However, Nigeria's winning goal came with eight minutes left as Uzoenyi's cross was horribly spilled by keeper Nadir Lamyaghri. The ball fell into the path of Ibrahim and, from two yards, he gifted Nigeria a place in the last four.