The last of the young soccer players rescued from a Thai cave were evacuated just in the nick of time before water pumps failed and the cavern reflooded, divers revealed Wednesday, as new footage of the kids’ rescue and recovery emerged.

About 100 workers — including the heroic Thai navy SEALs who stayed inside with the kids for a week — were still cleaning up inside the Tham Luang cave Tuesday night when the main pump stopped working, leading water levels to rise rapidly as people fled, three Australian divers told The Guardian.

“The screams started coming because the main pumps failed and the water started rising,” said one diver, who wouldn’t give his name. “The water was coming … It was noticeably rising.”

Everyone made it out safely, said the divers — a team drawn from Australia’s police and navy who helped haul the 12 boys and their soccer coach out on stretchers during the three-day rescue effort.

The kids — who’d been in the cave since they were trapped there by rising floodwaters on June 23 and were found July 2 — were largely asleep as dozens of rescuers carried the stretchers through the craggy cavern, one volunteer said.

“Some of them were asleep, some were wiggling their fingers . . . [as if] groggy, but they were breathing,” commander Chaiyananta Peeranarong, a former Thai navy SEAL who helped ferry out the stretchers, told AFP.

Thailand’s prime minister said Tuesday that the boys were given anti-anxiety medication to keep them calm but denied reports they were “sedated.”

It isn’t clear just how zonked out the boys were for the first and most dangerous leg of the trip — through the part of the cave complex that was still filled with water. The Aussies told the Guardian that each kid was tethered to a specialist diver for that portion — which involved squeezing through a hole only a few feet wide.

US rescuers admitted Wednesday they were shocked all of the kids made it out alive. “We were fully expecting casualties,” US Air Force Maj. Charles Hodges, who was involved in the mission, told “Today.”

“I felt like I was being optimistic when I told the governor of Chiang Rai when he asked, that in my mind, the potential chance of success was anywhere from 60 to 70 percent.”

But doctors say the kids are not only alive, they’re doing great.

Footage released Wednesday shows the boys waving and flashing two-finger V-for-victory signs from their beds in a hospital isolation ward — as their emotional parents wave back on the other side of the glass.

“Don’t need to worry about their physical health and even more so for their mental health,” Chaiwetch Thanapaisal, director of the Chiang Rai Prachanukroh hospital told reporters. “Everyone is strong in mind and heart.”