The Atlas Lions, also the first Arab team to make the last four, added Portugal to their list of European scalps with a resolute second-half performance.
Cristiano Ronaldo has likely played his last World Cup match after making an ineffectual appearance as a second-half substitute, failing to score his first knockout-phase goal at the tournament.
Morocco took a 42nd-minute lead through En-Nesyri’s header as they came on strongly late in the first period.
Portugal piled on the pressure after the break but an injury-hit Morocco, without three of their four first-choice defenders once skipper Romain Saiss was forced off, hung on despite being reduced to 10 men in added time.
The partisan crowd undoubtedly helped the North Africans over the line, although Portugal struggled to create many clear-cut chances as they lost a World Cup quarter-final for the first time.
Five-time Ballon d’Or winner Ronaldo was sent on early in the second half by coach Fernando Santos but could not rescue the Euro 2016 winners.
Instead it was another famous day for Morocco, after previous upset wins over Belgium and Spain, on penalties, in Qatar.
Walid Regragui’s men will face England or holders France in the semi-finals at Al Bayt Stadium on Wednesday.
Morocco will believe they can topple either of those sides, having still only conceded once at this World Cup.
Portugal enjoyed the majority of possession in the opening half-hour, but Joao Felix’s fifth-minute diving header, saved by goalkeeper Yassine Bounou, was the closest either side came to an early goal.
Morocco always looked dangerous, though, and Selim Amallah ballooned a shot over and Sofiane Boufal curled straight at Diogo Costa for the Africans.
It was no surprise when Morocco did forge ahead shortly before the interval.
Portuguese goalkeeper Costa came for a cross from left-back Yahya Attiat-Allah he was never going to get to and Sevilla striker En-Nesyri did the rest with a towering header.
Portugal almost equalised seconds later in spectacular fashion when Bruno Fernandes crashed a bouncing ball from the right-hand side of the box against the crossbar.
A crazy few minutes before half-time also saw Attiat-Allah, only playing in place of the injured Noussair Mazraoui, blaze wide after another rapid counter.
Portugal’s players were still appealing for a penalty after Fernandes threw himself to the ground under pressure from Achraf Hakimi at the other end.
Morocco almost doubled their advantage four minutes after the restart as Costa just about managed to prevent Hakim Ziyech’s free-kick from the right from flying all the way in.
Santos wasted little time in summoning for his captain, sending on Ronaldo and Joao Cancelo in the 51st minute.
Morocco, already without injured defenders Mazraoui and Nayef Aguerd, were dealt another blow when Saiss was stretchered off.
Portugal started to lay siege to the opposition goal and Fernandes came close to levelling again when he fired narrowly over from the edge of the box.
Morocco were largely keeping Portugal at bay, but Bounou had to be at his best to tip over Felix’s drive with eight minutes remaining.
Eight minutes of injury time were largely spent with Portugal camped in the Morocco half and Ronaldo almost had his moment when his low strike was kept out by Bounou.
Morocco substitute Walid Cheddira was harshly sent off for a second yellow card in the space of two minutes.
Zakaria Aboukhlal should have put the game to bed only to dink straight at Costa when clean through.
But his team somehow held onto their lead to write their names in the history books as Pepe headed wide from close range right at the death.
Follow us on Twitter at @thesignalng
Copyright 2022 SIGNAL. Permission to use portions of this article is granted provided appropriate credits are given to www.signalng.com and other relevant sources.