A daring rescue mission in the treacherous confines of a flooded cave in northern Thailand has saved all 12 boys and their soccer coach who were trapped deep within the labyrinth, ending a gruelling 18-day ordeal that claimed the life of an experienced diver and riveted people around the world.

Key points: "We are not sure if this is a miracle, a science, or what," Thai Navy SEALs say

"We are not sure if this is a miracle, a science, or what," Thai Navy SEALs say Boys and their coach are treated at a hospital

Boys and their coach are treated at a hospital Eight boys freed on Sunday and Monday are "healthy and smiling"

Thailand's Navy SEALs, who were central to the rescue effort, said on their Facebook page the remaining four boys and their 25-year-old coach were all brought out safely early on Tuesday evening (local time).

Several hours later, a medic and three SEAL divers who had stayed for days with the boys in their tiny refuge in the cave also came out.

Eight of the boys were rescued 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," the SEALs said.

"All the thirteen Wild Boars are now out of the cave. Everyone is safe."

Sorry, this video has expired Volunteers celebrate as news emerges that the rescue mission has been completed

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.

Loading

"We did something nobody thought possible," Chiang Rai province acting Governor Narongsak Osatanakorn, leader of the rescue effort, said at a celebratory news conference.

"It was mission possible for team Thailand.

"This mission was successful because we had power. The power of love. Everybody sent it to the 13."

Helicopters transporting the boys roared overhead.

People on the streets cheered and clapped when ambulances ferrying them on the last leg of their journey from the cave arrived at a hospital in Chiang Rai city.

Sorry, this video has expired Onlookers cheer as ambulances bring the last of those rescued from the cave to hospital

Thais also turned to social media to show their elation using the hashtag #Hooyah — a word used by the navy to build morale. Other hashtags included #Heroes and #Thankyou.

"You are our heroes!" some wrote, captioning cartoons showing the boys and their coach with dozens of rescue workers, volunteers and military personnel.

A visibly emotional Rachapol Ngamgrabuan, an official at Chiang Rai's provincial press office, said he was "very happy to see all Thai people love each other".

Rescue workers begin packing up after days inside the cave. ( Reuters: Soe Zeya Tun )

Payap Maiming, who helped provide food and necessities to rescue workers and journalists, said a "miracle" had happened.

"I'm happy for Thais all over the country," he said.

"And actually just everyone in the world because every news channel has presented this story and this is what we have been waiting for.

"It's really a miracle."

Volunteers celebrate at a press centre near Tham Luang cave complex. ( Reuters: Soe Zeya Tun )

Three days of intricate operations

The plight of the boys and their coach has captivated not only Thailand, but 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 Tham Luang cave on June 23, when they were exploring it after a soccer practice session and it became flooded by monsoon rains.

Each of the boys, aged 11 to 16 and with no diving experience, was guided out by a pair of divers in three days of intricate and high-stakes operations.

The boys were guided through the cave complex by experienced divers. ( AP: Tham Luang Rescue Operation Centre )

The route, in some places just a crawl space, had oxygen canisters positioned at regular intervals to refresh each team's air supply.

Highlighting the dangers, a former Thai Navy SEAL died on Friday while replenishing the canisters.

Cave-diving experts had warned it was potentially too risky to dive the youngsters out.

Volunteers react after hearing all boys were rescued from the Tham Luang cave. ( Kyodo via Reuters )

But Thai officials, acutely aware the boys could be trapped for months by monsoon rains that would swell waters in the cave system, seized a window of opportunity provided by relatively mild weather.

A massive water pumping effort also made the winding cave more navigable.

The confidence of the diving team, and expertise specific to the cave, grew after its first successful mission.

Loading

Loading

Anti-anxiety medication helped with rescue

Sorry, this video has expired Medics run to board helicopter with what appears to be one of the rescued boys

Thai Prime Minister Prayuth Chan-ocha, speaking before the final rescue was completed, said the boys were given an anti-anxiety medication to help with their perilous removal from the cave.

"It's called anxiolytic, something to make them not excited, not stressed," Mr Prayuth said.

Mr Prayuth said the Tham Luang cave would be closed for some time to make it safe for visitors.

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

They were given a treat on Tuesday: bread with chocolate spread that they had requested.

Photos of the first four boys to be rescued on a noticeboard in Chiang Rai hospital

Jedsada Chokdumrongsuk, permanent secretary at the Public Health Ministry, said the first four boys rescued were able to eat normal food, though they couldn't yet take the spicy dishes favoured by many Thais.

Two of the boys possibly have a lung infection but all eight are generally "healthy and smiling", he said.

"The kids are footballers, so they have high immune systems," Mr Jedsada said.

"Everyone is in high spirits and is happy to get out. But we will have a psychiatrist to evaluate them."

A patient list revealing the identity of the second group that was rescued from the cave. ( Supplied )

Boys invited to watch Manchester United

Loading

It could be at least seven days before they can be released from the hospital, Mr Jedsada told a news conference.

Family members have seen at least some of the boys from behind a glass isolation barrier.

If medical tests show no dangers, after another two days, parents will be able to enter the isolation area dressed in sterilised clothing and staying two metres away from the boys, said Tosthep Bunthong, a public health official.

One of soccer's most popular teams, Manchester United, expressed its relief over the rescue and invited the boys and their coach, as well as those who saved them, to come see them play on their home ground this season.

"Our thoughts and prayers are with those affected," a message posted on the English Premier League club's Twitter account said.

"We would love to welcome the team from Wild Boars Football Club and their rescuers to Old Trafford this coming season."

The international soccer federation, FIFA, had already invited the boys to attend the World Cup final in Russia this Sunday.

However, doctors treating the boys said it would be too soon for them to make the trip.

Relatives of trapped boys at a check point near the Tham Luang cave complex. ( Reuters: Soe Zeya Tun )

AP