In this undated photo released by Royal Thai Navy on Saturday, July 7, 2018, Thai rescue team members walk inside a cave where 12 boys and their soccer coach were trapped since June 23, in Mae Sai, Chiang Rai province, northern Thailand. (Royal Thai Navy via AP)

All 12 boys and their soccer coach have been rescued from a flooded cave in northern Thailand, the Thai navy SEALs said Tuesday, ending an 18-day ordeal that riveted people around the world.

The SEALs, who were central to the rescue effort, said on their Facebook page that the remaining four boys and their 25-year-old coach were all brought out safely Tuesday. Eight of the boys had been brought out of the cave by a team of Thai and international divers on Sunday and Monday.

“We are not sure if this is a miracle, a science, or what. All the thirteen Wild Boars are now out of the cave,” the SEALs said, referring to the name of the boys’ soccer team.

They said they were waiting for a medic and three SEALs who had stayed with the boys in their dark refuge deep inside the cave complex to come out.

Cheers erupted at a local government office where dozens of volunteers and journalists were awaiting news of whether the intricate and high-risk rescue mission had succeeded. Helicopters taking the boys to a hospital roared overhead.

Highlighting the dangers, a former Thai navy SEAL died Friday while replenishing oxygen canisters laid at regular intervals along the route out of the sprawling Tham Luang cave.

The plight of the boys and their coach has riveted Thailand and much of the world — from the heart-sinking news that they were missing to the first flickering video of the huddle of anxious yet smiling boys when they were found 10 days later by a pair of British divers. They were trapped in the cave that became flooded by monsoon rains while they were exploring it after a soccer practice on June 23.

The eight boys brought out by divers on Sunday and Monday were doing well and were in good spirits, a senior health official said Tuesday.