Hundreds of thousands of people protested against Ban Ki-moon in Rabat on Sunday after Morocco's government criticised the UN chief for remarks made on the disputed Western Sahara region.
Carrying banners denouncing Ban's alleged "lack of neutrality", protesters waved Moroccan flags and sang the national anthem. Official news agency MAP said more than three million people had participated.
Ban sparked anger in Morocco earlier this month when he visited a camp in Algeria for refugees from Western Sahara, which is under Moroccan control.
The UN has been trying to oversee an independence referendum for Western Sahara since 1992 after a ceasefire was reached to end a war that broke out when Morocco sent its forces to the former Spanish territory in 1975.
The Algerian-backed Polisario Front is seeking independence for the territory, a demand ruled out by Morocco.
During his visit, Ban announced plans to re-launch UN-sponsored talks between Rabat and the Polisario Front.
Ban said the UN mission to the region was "prepared to hold a referendum if there is agreement between the parties".
The Moroccan government, in a statement issued by the foreign ministry, accused Ban of speaking out of line and of using the word "occupation" to describe the status of Western Sahara.
"The Moroccan government noted with astonishment that the secretary general used the expression 'occupation' to describe Morocco's restoration of its territorial integrity," the statement said.
It said the alleged use of the word was "an insult to the government and the Moroccan people".
"Far from achieving the stated goal of his visit to relaunch political dialogue, the secretary general's comments could jeopardise the process," the statement added.
Ban rejected the criticism, insisting that he and the UN are "neutral partners" in efforts to find a solution to Western Sahara.
Ban visited the camp as part of a regional tour which also took him to Burkina Faso, Mauritania and Algeria, but not to Morocco.