Spain fans were dejected to see their team crash out of the tournament.
"We go back to Spain crying and we will have to reflect," said Julia Calvet, 21, in a Barcelona bar.
In Barcelona, Morocco fans packed into the central Raval neighbourhood to celebrate, lighting flares and singing.
"We needed to win so that Moroccans have confidence in themselves in Spain," Anass, a 22-year-old Moroccan cook told Reuters in downtown Barcelona.
Spain retained the enclaves of Melilla and Ceuta, which were previously colonial territories, after Morocco gained independence in 1956.