The Thai soldier who killed at least 20 people and holed up in a mall overnight was shot dead on Sunday morning by commandos, ending a near-17-hour ordeal which left dozens wounded and stunned the country.

The soldier killed at least 20 people and injured 42 others in a livestreamed mass shooting in a popular shopping mall.

Commandos from elite Thai police units killed the gunman, a police spokesman said, after an operation involving hundreds of security personnel.

The shooter, identified by police as Sergeant Major Jakapanth Thomma, remained inside the Terminal 21 shopping mall through the night, armed with assault weapons stolen from his barracks.

"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.