But he also said the best way to manage and resolve the territorial dispute would be "a responsible dialogue that guarantees our sovereignty rights and which take Spain's interests into account.
"The best way to settle and resolve this territorial conflict requires the virtues of an honest, frank and open dialogue on the future".
On Wednesday, Abbas El Fassi, the Moroccan prime minister, compared Spain's control of the two enclaves to the Israeli-Palestinian conflict.
"Along with Israel, Spain is today the only nation reluctant to turn the page on occupation" of territory, he said in an interview in the daily Aujourd'hui le Maroc.
Moroccans demonstrated against the trip and the government last week recalled its ambassador from Spain in protest.
On Monday, around 1,000 Moroccans held a demonstration against the opening day of Juan Carlos's visit at the Moroccan border post with Ceuta, with one banner reading: "King Juan Carlos, Get Out Of Morocco's Ceuta and Melilla."
"Spain must understand that its colonial era is over and for good," El Fassi said.
Miguel Angel Moratinos, the Spanish foreign minister, sought to soothe over the row stressing that Madrid wanted the "best possible relations" with Rabat.
"We hope we can continue to work together," Moratinos said in Lisbon where he was attending a regional meeting of foreign ministers.
"Ties between Morocco and Spain are solid. I hope that will allow us to maintain a normal relationship."
In a speech at Melilla city hall, Juan Carlos said it was his "duty" as the king of Spain to visit the enclaves which are "an integral part of our national territory".
Jose Luis Rodriguez Zapatero, the Spanish prime minister, visited the two enclaves last year - the first official visit by a Spanish prime minister since the early 1980s.
His trip also drew criticism from Rabat.
Ceuta has been in Spanish control since 1580 and Melilla since 1496.
Both were kept as military bases due to their strategic location on the Mediterranean coast.
Ceuta is situated about 50km east of Tangiers, facing Gibraltar across the narrow straits.
Melilla, further east along the coast, is about around 12.5sq km. It has a population of 57,000 and is 40 per cent Muslim.