The boy had fallen 32 metres down a dry well. It was hours before his parents realised where he was and alerted the authorities.
Moroccan officials announced Rayan's death at about 10pm local time, shortly after rescue workers were able to extract him from the well.
The painstaking five-day operation to reach him was broadcast around the world.
Rayan’s parents were escorted to an ambulance at the site before he emerged.
His body was wrapped in a yellow blanket after it was taken out through a tunnel dug specifically for the rescue, AP reported.
King Mohammed VI of Morocco expressed his condolences to Rayan’s parents in a phone call. A statement from the royal court confirmed the death.
Footage on social media showed the scene after the body was recovered, with hundreds of distraught rescue workers and onlookers gathered at the site, praying and shining the flashlights of their phones into the air.
Rayan fell down the well in the town of Tamorot on Tuesday, sparking a round-the-clock rescue involving digging a hole alongside the narrow shaft to reach him.
Search crews first used five bulldozers to dig vertically to a depth of more than 31 metres, Morocco’s official MAP news agency reported. Then on Friday they started excavating a horizontal tunnel to reach the trapped boy. Experts in topographical engineering were called upon for help.
As workers inched closer to the trapped child on Saturday, the sound of earth falling was heard, near the area where rescuers were at work.
One workman was seen walking away from the area holding his face as onlookers called out prayers.
Rescuers were able to feed Rayan food and oxygen through an opening in the well, and they placed a camera on him to watch his progress.
Hundreds of technical experts, workers, journalists, volunteers and well-wishers watched the rescue mission at the site and on live streams.
“I pray and beg God that he comes out of that well alive and safe,” his mother Wassima Kharchich had told the Moroccan news outlet 2M. “Please God, ease my pain and his, in that hole of dust.”
Journalists broadcasting the rescue mission on Facebook Live filled the air with prayers for Rayan and praised the sense of unity that the tragedy had brought among Moroccans.
A reporter for local outlet ChoufTV welled up with tears as he described the scene, and often expressed feeling a tightness in his chest as the tension mounted in anticipation of Rayan's appearance through the mouth of the tunnel.
The final phase of the rescue mission was the most dangerous.
Topography experts assisted with the technical side of the delicate operation, during which soil collapsed at various stages.
Members of Morocco's Auxiliary Forces cordoned off the area to keep crowds and reporters at a safe distance.
Overnight on Thursday, more rescuers, including members of the Moroccan Red Crescent, joined the operation and dug under floodlights.
An official from the rescue team said on Thursday that as the well became deeper, it also narrowed to a diameter of a little more than 30 centimetres.
Rayan's father, Khalid, said his nephew had, through the camera, seen the boy eating. He said he was touched by the outpouring of support for Rayan's ordeal.
"Many people did not sleep, they stayed up with us and I thank them," he told reporters on Thursday.
The hashtag #SaveRayan in Arabic and English trended across North Africa throughout the operation as tributes from diplomats, world leaders and celebrities flooded social media.
The family was yet to announce the date of the funeral, but according to Muslim tradition it must take place soon, in principle as early as Sunday.