The rescued boys emerged as night fell from the Tham Luang cave complex after navigating a treacherous escape route of more than four kilometres (2.5 miles) through twisting, narrow and jagged passageways.
Their escape led to an explosion of jubilation on social media in Thailand and around the world as the rescued boys were rushed to hospital.
Rescue chief Narongsak Osottanakorn said the four had been taken to hospital and were "safe".
A defence ministry official earlier said that six boys had "come out". But Narongsak and the Thai Navy SEALS, who are involved in the rescue and have regularly posted updates about the operation on their Facebook page, reported only four were out of the cave.
Foreign elite divers and Thai Navy SEALS began the complex operation to extract the 12 boys and their football coach on Sunday morning as they raced against time, with imminent monsoon rains threatening more flooding that would doom the mission.
"Today is the D-day. The boys are ready to face any challenges," rescue chief Narongsak Osottanakorn told reporters near the cave site on Sunday morning.
The group became trapped in a cramped chamber deep inside Tham Luang in a mountainous area of northern Thailand on June 23, when they went in after football practice and got caught behind rising waters.
Their plight transfixed Thailand and the rest of the world, as authorities struggled to devise a plan to get the boys -- aged between 11 and 16 – and their 25-year-old coach out.
’Mission Impossible’
The rescue of the initial batch of boys was a stunning victory in an operation Narongsak had earlier dubbed "Mission Impossible", and led to cautious optimism that the others would also be saved.
The group was found dishevelled and hungry by British cave diving specialists nine days after they ventured in.
Initial euphoria over finding the boys alive quickly turned into deep anxiety as rescuers struggled to find a way to get them out.
The death of a former Thai Navy SEAL diver who ran out of oxygen in the cave on Friday underscored the danger of the journey even for professionals.
After a short deluge of rain on Saturday night and with more bad weather forecast, Narongsak said on Sunday morning authorities had to act immediately.
"There is no other day that we are more ready than today," he said.
"Otherwise we will lose the opportunity."
Between the base camp operated by Thai Navy SEALS inside the cave and the trapped boys were twisting, turning passageways with torrents of water gushing through.
The water in the cave was muddy and unclear, with one diver comparing it to a cafe latte. Ropes were installed to help guide the boys through the darkness. Narongsak said Sunday morning two divers would escort each of the boys out of the cave.
Rescue efforts
Officials had looked at many different ways to save the boys and their coach.
One early potential plan was to leave them there for months until the monsoon season ended and the floods subsided completely, but that idea was scrapped over concerns about falling oxygen levels and waters rising too high.
More than 100 exploratory holes were also bored -- some shallow, but the longest 400 metres deep -- into the mountainside in an attempt to open a second evacuation route and avoid forcing the boys into the dangerous dive. American technology entrepreneur Elon Musk even deployed engineers from his private space exploration firm SpaceX and Boring Co. to help.
Meanwhile rescuers fed a kilometres-long air pipe into the cave to restore oxygen levels in the chamber where the team was sheltering with medics and divers.
Emotional notes
On Saturday, Thai Navy SEALs published touching notes scrawled by the trapped footballers to their families, who had been waiting for them agonisingly close by outside the cave entrance.
The boys urged relatives "not to worry" and asked for their favourite food once they were safely evacuated, in notes handed to divers.
In one, Pheerapat, nicknamed "Night", whose 16th birthday the group were celebrating in the cave when they became stuck on June 23, said: "I love you, Dad, Mum and my sister. You don’t need to be worried about me." — AFP/VNS