Huge crowds on Monday laid to rest Rayan, a five-year-old Moroccan boy who spent five days trapped down a well and sparked a rescue operation that gripped the world but ended in tragedy.

Hundreds stood to mourn in a cemetery in the remote forested hills of the northern Chefchaouen region, a few kilometres (miles) from the site of the accident, as an imam read funeral prayers.

Moroccan emergency services teams carry the body of five-year-old Rayan into an ambulance after pulling him from a well shaft
Moroccan emergency services teams carry the body of five-year-old Rayan into an ambulance after pulling him from a well shaft AFP / Fadel SENNA

The boy's small coffin, draped in a green sheet covered with Koranic scriptures, was then laid in the ground to the sound of religious chanting.

The boy had fallen down a narrow, 32-metre (100-foot) dry well last Tuesday, sparking a complex earth-moving operation to try to extract him without triggering a landslide.

Well-wishers had flooded social media with messages of sympathy and prayers that he would be brought out alive.

Map locating the village Ighrane where emergency crews found a five-year-old boy dead at the bottom of a well in a painstaking five-day rescue operation
Map locating the village Ighrane where emergency crews found a five-year-old boy dead at the bottom of a well in a painstaking five-day rescue operation AFP / Emmanuelle MICHEL

On Saturday night, crowds had cheered as rescue workers cleared away the final handfuls of soil to reach him, the end of a marathon digging operation in the village of Ighrane in Morocco's Rif mountains.

But their joy turned to grief when the royal cabinet of the North African nation announced that the boy was dead.

King Mohammed VI had called the parents to voice his condolences.

Crowds conveged on the site to watch the rescue operation, offering songs of support and prayers to the workers
Crowds conveged on the site to watch the rescue operation, offering songs of support and prayers to the workers AFP / Fadel SENNA

Ighrane resident Mounir Mourid said Monday that the village was in shock.

Graphic on the failed rescue operations to save a a five-year-old child who fell into a well in Morocco
Graphic on the failed rescue operations to save a a five-year-old child who fell into a well in Morocco AFP / Emmanuelle MICHEL

"Rayan is carved forever on our hearts," he said.

"It's like he shone a light on the situation in our region," he added.

"Here, we've never seen the media or politicians. We don't have a phone signal, roads, hospitals or schools."

Graphic on the failed rescue operations to save a a five-year-old child who fell into a well in Morocco
Graphic on the failed rescue operations to save a a five-year-old child who fell into a well in Morocco AFP / Emmanuelle MICHEL

Rayan's father Khaled Aourram said he had been repairing the well near the family home when his son fell in.

The shaft, just 45 centimetres (18 inches) across, was too narrow for rescuers to reach Rayan directly, and widening it was deemed too risky -- so earth movers dug a wide slope into the hill to reach him from the side.

Rescue crews, using bulldozers and front-end loaders, excavated the surrounding red earth down to the level where he was trapped, before drill teams carefully dug a horizontal tunnel, under the constant threat of a landslide.

Vast crowds came to offer their support, singing and praying to encourage the rescuers who worked around the clock -- but to no avail.

Mourad Fazoui, in Morocco's capital Rabat, was one of millions mourning the tragedy.

"May his soul rest in peace and may God open the gates of heaven to him," the salesman said.

Social media across the Arab world was full with messages of support, grief, and praise for rescue workers.

Even citizens of Morocco's regional rival Algeria voiced their sadness, including national football coach Djamel Belmadi.

One social media user wrote that the child had "succeeded where leaders and the media have failed" in that he "united people around him."

But one deplored a "dystopian world" where "Arab nations are moved" by the Morocco rescue operation, while thousands of children die in conflict or of hunger in Yemen and Syria.

Noureddine Moudiane, a pro-government member of parliament, called on the authorities to "put an end to informal well-digging, which often doesn't follow the relevant legal procedures".