Midfielder Hakim Ziyech scored one goal and set up another as the Atlas Lions looked lively in the build-up match. Ziyech gave Morocco the lead as he converted a 29th minute penalty and he delivered an inch-perfect cross for Khalid Boutaib to head the winner after Dusan Tadic had equalised.
Morocco also achieved the victory without star player Achraf Hakimi, who was withdrawn from the squad due to injury. Hakimi, who plies his trade in the Spanish La Liga for Real Madrid, sustained an injury during Morocco's first training session in Turin ahead of the clash with Serbia.
Serbia started the match against Morocco with three forwards and two attacking midfielders, but the unfamiliar strategy left anchorman Nemanja Matic with too much work to do and it was not a surprise when Ziyech made no mistake with the spot-kick after Nikola Maksimovic hauled down Nordin Amrabat.
The 1976 African champions will launch their Group B campaign in Russia with a clash against Iran in St Petersburg on June 15. Morocco and Iran have played each other twice in the past with the Atlas Lions winning both matches including one on penalties.
Morocco, who under Frenchman Herve Renard completed their qualifying campaign with maximum points and no goals conceded, last appeared in a World Cup in 1998 in France.
Germany 1-1 Spain
A superb Thomas Mueller equaliser saw world champions Germany fight back against Spain as the European powerhouses drew 1-1 on Friday.
After Rodrigo Moreno put Spain ahead after just six minutes, Germany went into the break level as Mueller hit his 38th goal for the national team on his 90th international appearance.
The draw in front of a sold-out 50,653 crowd preserves both sides' respective unbeaten runs dating back to Euro 2016.
Spain coach Julen Lopetegui included three veterans who won the 2010 World Cup in Gerard Pique, Sergio Ramos and Andres Iniesta. At 33, Iniesta gave the Germany midfield a masterclass during the first 45 minutes, moving the ball at ease with silky passes at the heart of Spain's attack.
Germany boss Joachim Loew started with six World Cup winners from 2014 in Mats Hummels, Jerome Boateng, Sami Khedira, Toni Kroos, Mueller and Mesut Ozil.
Portugal 2-1 Egypt
Portugal’s captain, Cristiano Ronaldo, scored two stoppage-time goals to steal a 2-1 win over Egypt after Mohamed Salah had given the African side the lead in an entertaining World Cup warm-up in Switzerland.
Salah scored with a thumping drive to put Egypt ahead early in the second half. However, Ronaldo refused to be upstaged and he sneaked in behind his marker to head in Ricardo Quaresma’s cross in the 92nd minute.
Not content with that, Ronaldo scored again with another header, once more from a Quaresma pass, with the final move of the game. There was initial confusion as the referee consulted the video assistant referee (VAR) system for a possible offside before allowing the goal.
Next up for Portugal is the Netherlands - who fell to a defeat against England on Friday evening - while Egypt face Greece.
Tunisia 1-0 Iran
Iran went home disappointed after losing to a fortunate goal by Tunisia on Friday.
Iran were looking to kick-start their preparations for the World Cup final on a good note but an own goal settled the friendly clash on a waterlogged pitch in Tunis.
Wahbi Khazri proved the talisman for the home team and it was his dribbling that led to the 71st minute winner. Khazri's shot was parried by Iran goalkeeper Alireza Beiranvand into the path of defender Milad Mohammadi, who could only turn the ball into his own net.
Iran were using the match to prepare to face the playing style of Morocco, who share their group in Russia with Portugal and Spain.
A dipping free-kick from captain Ehsan Hajsafi in the 67th minute, saved by Tunisia's Farouk Ben Mustapha, was the closest Iran came to scoring.