Ghana's Baffour Guyan was unable to inspire his side to victory
Morocco and Ghana both exited the men's Olympic football tournament on Wednesday.
Ghana lost 1-0 to Japan to leave them in third place in Group B, behind winners Paraguay who beat second-placed Italy 1-0.
In Group D, Morocco beat Iraq 2-1 but were denied a place in the quarter-finals by Costa Rica, whose 4-2 win over Portugal gave them a superior goals tally to the North Africans.
The results mean that Iraq top Group D while Costa Rica, who scored their fourth goal only in injury time, finish as runners-up.
Like Morocco, Ghana ended level on points and goal difference with the team immediately above them and go out having scored fewer goals.
Playing in a virtually empty stadium, Morocco dominated large parts of the match with Bouabid Bouden hitting the post and Merouane Zemmama also coming close.
However, they fell behind in the 63rd minute when Iraq substitute Emad Mohammed burst past two defenders and hit a low cross for Salih Sadit to steer home for his second goal of the tournament.
The Moroccans were deservedly level five minutes later when striker Farid Talhaoui was fouled in the area by Saad Attiya.
Argentinian referee Horacio Elizondo pointed to the spot and Bouden, who plays his club football with Lens in France, buried the ball in the corner.
In the 77th minute, Morocco gave themselves a lifeline when the impressive Zemmama sped away down the right and his cross fell invitingly for the unmarked Salaheddine Aqqal to score.
Despite the win, they were denied by an injury-time goal from Pablo Brenes for Costa Rica.
Ghana, who needed just a point to advance, went out when Yoshito Okubo headed home in the 37th minute.
Substitute Kwadwo Poku almost snatched an equalizer for Ghana with four minutes to go but his shot from a tight angle was pushed round the post by the Japanese goalkeeper.
It means that Mali are Africa's only chance of keeping the Olympic title on the continent.
In Saturday's quarter-finals, Mali take on Italy, Iraq face Australia while favourites Argentina are up against Costa Rica and South Korea meet Paraguay.