NAKHON RATCHASIMA, THAILAND -- A soldier with a grudge gunned down 26 people and wounded 57 in Thailand's worst mass shooting before he was fatally shot inside a mall in the country's northeast on Sunday, officials said.

Officials said the soldier was angry over a financial dispute, first killing two people on a military base and then went on a far bloodier rampage Saturday, shooting as he drove to the mall where shoppers fled in terror.

It took police sharpshooters 16 hours to end the crisis.

Authorities said Sgt. Maj. Jakrapanth Thomma was behind the attack in Nakhon Ratchasima, a hub for Thailand's relatively poorer and rural northeastern region. Much of the shooting took place at Terminal 21 Korat, an airport-themed mall filled with colorful Lego sculptures, a merry-go-round and huge replicas of landmarks from around the world.

"This incident was unprecedented in Thailand," Prime Minister Prayuth Chan-ocha told reporters as he gave the final tally Sunday morning after visiting the wounded in hospitals.

"I hope this is the only one and the last incident, and that it never happens again. No one wants this to happen. It could be because of this person's mental health in this particular moment," he said.

Prayuth said he was worried that people inside the mall could be accidentally hit by bullets fired by police, but added, "I have checked, that didn't happen."

Video taken outside the mall showed people diving for cover as shots rang out mid-afternoon Saturday. Many were killed outside the mall, some in cars, others while walking.

Nattaya Nganiem and her family had just finished eating and were driving away when she heard gunfire.

"First I saw a woman run out from the mall hysterically," said Nattaya, who shot video of the scene on her phone. "Then a motorcycle rider in front of her just ran and left his motorcycle there."

Hundreds of people were evacuated from the mall in small batches by police while they searched for the gunman.

"We were scared and ran to hide in toilets," said Sumana Jeerawattanasuk, one of those rescued by police. She said seven or eight people hid in the same room as her.

"I am so glad. I was so scared of getting hurt," she said.

Shortly before midnight, police announced they had secured the above-ground portion of the mall, but were still searching for the shooter. About 16 hours later, officials held a news conference outside the mall to announce the gunman was fatally shot.

Defence Ministry spokesman Kongcheep told Thai media that the first person killed was the commanding officer of the 22nd Ammunition Battalion, in which the suspect also served. He said the gunman had fired at others at his base and took guns and ammunition before fleeing in an army Humvee.

City and neighbourhood police officers, who asked not to be identified because they were not authorized to release information, said the man fired shots as he drove to the mall. Thai Rath television aired mall security camera footage showing a man with what appeared to be an assault rifle.

The man also posted updates to his Facebook page during the rampage.

"No one can escape death," read one post. Another asked, "Should I give up?" In a later post, he wrote, "I have stopped already."

A photo circulated on social media that appeared to be taken from the Facebook page shows a man wearing a green camouflaged military helmet while a fireball and black smoke rage behind him. Jakrapanth's profile picture shows him in a mask and dressed in military-style fatigues and armed with a pistol. The background image is of a handgun and bullets. The Facebook page was made inaccessible after the shooting began.

Terminal 21 Korat, a multi-level glass and steel mall, is designed to resemble an airport terminal, complete with a mock control tower and departure gates. A large model passenger jet dangles from wires beside one of the main escalators.

Each of its seven retail floors is decorated to represent a different country. A giant replica of Paris' Eiffel Tower soars to the ceiling, while a model of London's Big Ben dominates another area, and a massive model of California's Golden Gate Bridge spans an open courtyard. A two-story golden Oscar statue towers over a food court.

Many malls in Thailand, including Terminal 21's namesake in Bangkok, have metal detectors and security cameras at entrances manned by uniformed but unarmed security guards. Checks on those entering are often cursory at best.

In Bangkok, the original Terminal 21 in the bustling heart of the city was filled with shoppers as usual Sunday morning. There were no signs of increased security or commemoration of the tragedy that unfolded a few hours' drive away.

Gun violence is not unheard of in Thailand. Firearms can be obtained legally, and many Thais own guns. Mass shootings are rare, though there are occasional gun battles in the far south of the country, where authorities have for years battled a long-running separatist insurgency.

The incident in Korat comes just a month after another high-profile mall shooting, in the central Thai city of Lopburi. In that case, a masked gunman carrying a handgun with a silencer killed three people, including a 2-year-old boy, and wounded four others as he robbed a jewelry store. A suspect, a school director, was arrested less than two weeks later and reportedly confessed, saying he did not mean to shoot anyone.

------

Vejpongsa reported from Bangkok. Associated Press journalists Grant Peck, Preeyapa T. Khunsong and Adam Schreck contributed to this report.