A first-half goal from Youssef En-Nesyri proved to be the only goal of the game, with the Moroccans winning 1-0 – though they had to endure intense pressure for much of the second-half.
It means another three City stars’ World Cup has ended earlier than expected in a World Cup full of surprises and shocks.
Ruben Dias and Bernardo started the game, with Joao Cancelo on the bench.
Morocco continued to be the tournament’s surprise package with their energy and superb counter-attack quickly causing Portugal problems.
The Portuguese looked to unpick a defence that had conceded just one goal at this World Cup, but the first 45 looked likely to end in stalemate until the final few moments when the game exploded into life.
Yahya Attiat-Allah’s burst down the left ended with a wonderful cross into the box where Youssef En-Nesyri leapt to meet the ball ahead of a defender and keeper Diogo Costa and head the Moroccans into the lead.
Portugal almost responded immediately when Bruno Fernandes sent an angled volley crashing against the crossbar in the time that remained, but it was the Africans who led at the break.
Cancelo was summoned from the bench – along with Cristiano Ronaldo – on 51 minutes in a bid to energise an attack that scored six goals in their last game.
And as the half wore on, so Morocco became more and more pinned in their own final third, desperately defending their narrow advantage.
Cancelo’s brief appeared to be whipping the ball in from the left to try and find Ronaldo’s head, while Bernardo continually looked for openings in and around a crowded Moroccan box.
And Bernardo came agonisingly close to levelling as a low pass from a free-kick found the City playmaker near the penalty spot, but there was a fraction too much pace on the ball and he failed to connect properly.
Fernandes and Joao Felix both came close to equalising as the clock ticked towards the last 10 minutes and Portugal’s continued pressure had the Moroccans clinging on.
As the game moved into added time, Morocco were reduced to ten men when Walid Cheddira was shown a second yellow card inside two minutes.
In the sixth minute of stoppage time, Pepe looked set to level as the ball came into the six-yard box, but he somehow headed wide with the goal at his mercy.
That was the last chance of the game as Morocco’s fairy-tale tournament continued and Portugal’s dream ended.