A soldier who killed 20 people in a gun rampage in the Thai city of Nakhon Ratchasima has been shot dead by the security forces, Thai police say.
Jakraphanth Thomma on Saturday killed his commanding officer before stealing weapons from a military camp.
The suspect continued his attack on the streets and in a shopping centre in Nakhon Ratchasima, also known as Korat.
The gunman, who posted material to social media, was shot dead after being cornered all night in the building.
His motives for his actions remain unclear.
"We don't know why he did this. It appears he went mad," Defence Ministry spokesman Kongcheep Tantrawanit told Reuters news agency.
Public Health Minister Anutin Charnvirakul posted on his Facebook page on Sunday morning, congratulating the security forces for their actions in the north-eastern Thai town.
"Thank you police and army for ending the situation. Shooter shot dead!!!"
Officials said that the death toll stood at 20, with 42 wounded, revising an earlier total of 21 dead.
"The official number of the dead right now is 20 and 42 wounded. Of them, 21 are still in hospital and 21 have gone home," said Narinrat Phitchayakhamin, head of the Provincial Office of Public Health in the town.
However, it is still possible that the number of fatalities could rise.
Shortly after 03:00 local time (20:00 GMT Saturday) gunfire was heard as the security forces raided the building, trying to dislodge the gunman.
Several people were led out of the centre, but there were concerns that more people were being held hostage.
At 09:30 (02:30 GMT) on Sunday, police confirmed that the gunman had been shot dead, but so far there are no more details about how the operation concluded.
Earlier reports said the gunman, aged 32, had tried to escape via the back of the building.
The suspect's mother was also brought to the shopping centre to try to persuade him to give himself up.
One of the people freed told the BBC how she and others hid in a bathroom on the fourth floor, before fleeing to the second and hiding under a restaurant table for three hours, hearing at least four gunshots before she saw some soldiers and could get to safety.
Charlie Crowson, a teacher of English who lives in Nakhon Ratchasima, told the BBC there were "bodies on the streets" of the normally peaceful town.
He said that one of his girlfriend's former students was among those killed in the attack that lasted for many hours.
How did the attack unfold?
It began at about 15:30 local time on Saturday (08:30 GMT) at the Suatham Phithak military camp, where the commanding officer, named by the Bangkok Post as Col Anantharot Krasae, was killed.
The Post said a 63-year-old woman, Col Anantharot's mother-in-law, and another soldier were also killed there.
The suspect seized arms and ammunition from the camp before taking a Humvee-type vehicle.
He then opened fire at a number of sites before arriving at Terminal 21 at about 18:00 local time (11:00 GMT).
Local media footage appeared to show the suspect getting out of his vehicle and firing shots as people fled.
CCTV footage showed him inside the shopping centre with a raised rifle.
What did the suspect post on social media?
He posted on his social media accounts during the attack, with one post on Facebook asking whether he should surrender.
He had earlier posted an image of a pistol with three sets of bullets, along with the words "it is time to get excited" and "nobody can avoid death".
Facebook has now taken the page down.
It said: "Our hearts go out to the victims, their families and the community affected by this tragedy in Thailand. There is no place on Facebook for people who commit this kind of atrocity, nor do we allow people to praise or support this attack."
Compiled by Olalekan Adeleye
BBC