Rescuers freed the last four of 12 Thai boys and their soccer coach from deep inside a flooded cave on Tuesday, a successful end to an extraordinarily perilous mission that gripped the world for more than two weeks.

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 transporting the boys roared overhead. People on the street cheered and clapped when ambulances carrying the boys on the last leg of their journey passed by.

"We are not sure if this is a miracle, a science, or what. All the 13 Wild Boars are now out of the cave," the SEALs said, referring to the name of the boys' soccer team. "Everyone is safe."

The team members, who range in age from 11 to 16, and the coach became trapped on June 23 while exploring the cave complex in the northern province of Chiang Rai after soccer practice and a rainy season downpour flooded the tunnels.

'We have done something that nobody thought we could do,' says regional governor of Chiang Rai 1:01

British divers found the group, hungry and huddled in darkness on a muddy bank in a partly flooded chamber several kilometres inside the complex, on Monday last week. After pondering for days how to get the 13 out, a rescue operation was launched on Sunday when four of the boys were brought out, tethered to rescue divers.

Another four were rescued on Monday, and the last four boys and the 25-year-old coach were brought out on Tuesday.

​

Amporn Sriwichai, an aunt of the rescued coach, Ekkapol Chantawong, said she was happy and excited. "If I see him, I just want to hug him and tell him that I missed him very much," she said.

Word that they had all made it out safely was met with cheers at a local government office where dozens of volunteers and journalists were awaiting the outcome of the rescue operation.

CBC's Briar Stewart describes celebratory atmosphere in northern Thailand after entire soccer team is safely brought out of Tham Luang cave 1:34

"There's just a lot of happiness in the atmosphere right now, people taking pictures, hugging, really just a sense of elation that the mission today was so successful," the CBC's Briar Stewart reported from a media centre there.

Payap Maiming, 40, 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, for the people of Mae Sai, and actually just everyone in the world because every news channel has presented this story, and this is what we have been waiting for," she said. Mae Sai is the district where the cave is located.

Onlookers wave as an ambulance carrying rescued schoolboys leaves a military airport. The boys are expected to remain in hospital for several days for observation. (Tyrone Siu/Reuters)

Volunteers from as far away as Australia and the United States helped with the effort to rescue the boys. U.S. military

personnel also helped.

People across Thailand, and around the world, have been closely following the rescue operation, including at the Mae Sai Prasitsart school where six of the trapped boys are students.

However, celebrations will be tinged with sadness over the loss of a former Thai navy diver who died last Friday while on a resupply mission inside the cave in support of the rescue.

This group has been working to pump water out of the cave for more than a week and are all smiles after learning the entire team is out safely. <a href="https://t.co/Dog4BN4EfQ">pic.twitter.com/Dog4BN4EfQ</a> —@briarstewart

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

But Thai officials, acutely aware that 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.

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

The last five were brought out of the cave on stretchers, one by one over the course of Tuesday, and taken by helicopter to hospital.

(CBC)

Three members of the SEAL unit and an army doctor, who had stayed with the boys since they were found, made it out of the cave later on Tuesday, officials said.

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

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

Officials have not been commenting on the rescue mission as it has been taking place, so the condition of those brought out on Tuesday wasn't clear.

Watch an interview with one of the divers involved in the daring rescue operation.

Ivan Karadzic was part of the dive team that rescued boys and soccer coach trapped in cave 7:11

The eight boys brought out on Sunday and Monday were in good health overall and some asked for chocolate bread for breakfast, officials said, though they can't yet digest the spicy dishes favoured by many Thais.

Two of the boys had suspected lung infections but the four boys from the first group rescued were all walking around in hospital.

Motorists pass by a billboard displaying a photograph of the Thai children's Wild Boar soccer team with a message reading 'Welcome home brothers' in Chiang Rai following their rescue. (Tang Chhin Sothy/AFP/Getty Images)

The boys were still being quarantined from their parents because of the risk of infection and would likely be kept in hospital for a week to undergo tests, officials said earlier on Tuesday.

It was clear doctors were taking a cautious approach. Jedsada Chokdumrongsuk, permanent secretary at the public health ministry, said they were uncertain what type of infections the boys could face "because we have never experienced this kind of issue from a deep cave."