Footage posted on social media showed the scene after his body was recovered, with hundreds of distraught rescue workers and onlookers gathered at the site chanting to god and shining the flashlights of their phones into the air.
Rayan Awram, aged five, fell into the well at his village of Ighran in the hills near Chefchaouen on Tuesday, triggering a huge rescue effort that engrossed the country.
Rescuers finally reached him late on Saturday after removing much of the adjacent hillside and delicately tunnelling a horizontal passage into the well.
King Mohammed sent condolences to his parents, a statement carried by state media reported.
The well was only 45 cm (18 inches) wide at the top and tapered as it dropped 32 metres (100 feet) to the bottom, where Rayan was trapped, making it impossible for rescuers to descend directly.
A male relative of the boy said on Friday that the family had first realised he was missing when they heard muffled crying and lowered a phone with its light and camera switched on to locate him.