NAKHON RATCHASIMA, Thailand (AFP) -- The Thai soldier who killed at least 20 people and holed up in a mall overnight was shot dead Sunday morning by commandos, ending a near-17-hour ordeal which left dozens wounded and stunned the country.
It was unclear how many people remained trapped inside the Terminal 21 mall in Nakhon Ratchasima -- also known as Korat -- where the gunman held out through the night, armed with assault weapons stolen from his barracks.
Volleys of gunfire rang out as the siege ran into dawn, hours after Thai security services stormed the ground floor and freed scores of stunned, terrified shoppers from a bloody rampage that the gunman -- a junior army officer identified as Sergeant-Major Jakrapanth Thomma -- had relayed via Facebook posts.
"He was shot dead thirty minutes ago" (0200 GMT), chief of the Crime Suppression Division Jirabhob Bhuridej told AFP.
Commandos from elite Thai police units killed the gunman, a police spokesman added, after an operation involving hundreds of security personnel.
"The official death toll is 20 and wounded 42... nine are in surgery," Narinrat Pitchayakamin, a Korat doctor told reporters, revising down an initial death toll of 21.
But it was unclear if there were more victims inside the multi-level complex which was packed with Saturday shoppers when the gunman stormed in.
A fleet of ambulances left the front of the complex and forensic police poured into the grim crime scene, shortly before the gunman's death was confirmed.
The night was peppered with heavy exchanges of gunfire and sporadic evacuations.
A police officer who took part in a raid to flush out the gunman died, according to deputy prime minister Anutin Charnvirakul.
"He had been hit and unfortunately, he couldn't make it," said Anutin.
Shocked evacuees recounted how an ordinary Saturday shopping day at the busy mall descended into horror as the gunman entered.
"It was like a dream... I'm grateful I survived," Sottiyanee Unchalee, 48, told AFP, explaining she hid in the toilet of a gym inside the mall as she heard the gunfire.
"I'm so sorry for those who died... (and) the people still trapped inside."
Jakrapanth relayed his shooting spree through Facebook posts which charted the attack from the army barracks in the city to the mall, where an unknown number of shoppers remained trapped.
A volunteer rescue worker recounted a bloody scene of horror after his team carried four corpses to the hospital.
"I've never seen anything like this," Peerapong Chatadee told AFP.
"I just feel so sad. He is a soldier, he should not have fired at unarmed people."
The bloodshed began Saturday afternoon when Jakrapanth shot three people -- among them at least one soldier -- at a senior officer's house and then at the nearby army barracks, before driving an army vehicle to the town centre.
There the gunman used weapons stolen from the military arsenal to unleash carnage in the town centre.
He "used a machine gun and shot innocent victims resulting in many injured and dead", said police spokesman Krissana Pattanacharoen.
Throughout the day, Jakrapanth posted images of himself and wrote several posts on his Facebook page as the attack unfolded.
In one Facebook video -- since deleted -- the assailant, wearing an army helmet, filmed from an open-top jeep, saying, "I'm tired... I can't pull my finger anymore" as he made a trigger symbol with his hand.
There were also photos of a man in a ski mask holding up a pistol.
A Facebook spokesperson said: "We have removed the gunman's accounts from our services and will work around the clock to remove any violating content related to this attack as soon as we become aware of it."
The city is home to one of the largest barracks in Thailand, a country where the military is enmeshed in politics and society.
The nation also has one of the highest rates of gun ownership in the world, and several shootings at courthouses last year renewed concern about gun violence. (AFP)