CHIANG RAI, Thailand — After 17 long nights trapped in a dark and damp cave, four remaining boys and their soccer coach were freed one-by-one by divers in Thailand on Tuesday.

Rescuers started the day hoping to help the final five who had been stranded for more than two weeks reach the surface and safety. The first of the group emerged Tuesday afternoon local time with the others following soon after, ending an eight hour and 10 minute operation.

"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 Thai navy SEALs posted on Facebook, referring to the name of the boys' team.

The medical condition of the boys rescued Tuesday was not immediately known. Officials previously said that the healthiest boys would be brought out first.

Thai Navy SEALs also returned safely. Thai Navy

The complicated operation started Sunday when expert divers rescued the first four boys. They then went back in on Monday and retrieved four more.

With experience under their belts, Tuesday's operation was completed hours faster than before.

A medic and Thai navy SEALs who had stayed deep inside the mountain since the 13 missing males were found on July 2 also needed to navigate the narrow, water-logged passages in the cave complex. All successfully returned to the surface Tuesday.

"We have finished successfully something that nobody thought could be done," Chiang Rai Gov. Narongsak Osottanakorn told a news conference Tuesday after the operation was completed.

The boys vanished on June 23 with their 25-year-old coach after seasonal rains flooded the caverns and narrow passages, blocking their exit from the popular tourist destination. Nine days later, two expert cave divers found the trapped group.

A Thai-led international group of rescue and diving specialists then started planning the best way to free the group.

At 10 a.m. local time (11 p.m. ET Monday) on Tuesday, 19 divers entered the caves. Another 100 divers and rescuers, along with ambulances and other vehicles were in the area as the operation began.

The initial eight freed Monday are currently in good health, officials said. They were receiving medical treatment at a hospital in Chiang Rai.

For 10 days they sat in darkness, wondering if anybody would come. Then for 8 more they waited and assured their parents they were brave enough to make it out. An incredible rescue effort centered on volunteers made this happen. A great day to believe in amazing endings. @NBCNews — Janis Mackey Frayer (@janisfrayer) July 10, 2018

The successful feat had some of the divers, a group of Scandinavians, saying they were "very happy" and it was "mission accomplished," but it didn't come without risks to rescuers.

Last Friday, during the early stages of the rescue, a former Thai navy SEAL fell unconscious underwater and died.

"It was a tough dive. Tough week," one of the Scandinavian divers told NBC News. "Everything's cut," he said, referring to his feet. “We feel very happy. Now we need beer and sleep."

The boys were also recovering.

Jetsada Chokdumrongsuk, a public health official, told reporters that they were were given antibiotics, rabies vaccines and vitamin B2, and showed no signs of fever or vision problems.

They could communicate normally and were able to stand up, walk around and get dressed themselves, he said. They even chatted with Prime Minister Prayut Chan-o-cha when he came to the hospital.

X-rays revealed that two of the boys had suspected lung infections, but doctors were awaiting lab tests, Jetsada said.

In an effort to control infection, doctors were not allowing visitors. Positive test results would mean face-to-face visits between the boys and their families would be possible at the hospital.

So far, only the first group of four boys — ages 14 to 16 — have been able to see their families through glass windows, Jetsada said.

"They're homesick and they miss their parents, and they're very happy to be here," he added.

The boys were "often hungry" during their ordeal and they "need a lot of food," Jetsada said, adding that they have requested bread and chocolate spread. They've also been given soft, easily digestible medical-grade food that is high in nutrients.

After divers discovered the group in the caves last week, they were given energy gels and baby food to help their bodies slowly adjust to solids.

The second group, ages 12 to 14, were all hospitalized with low body temperatures, though they'd since stabilized.

It could be another week before the children are released from the hospital, officials said.

There was global jubilation at news of the rescue. World leaders, including President Donald Trump, tweeted their good wishes, and the Manchester United football team invited them to their stadium.

Rachel Elbaum reported from London, and Bill Neely from Chiang Rai, Thailand.