Somali police arrested him after the shooting in the capital, Mogadishu.
Everyone who died had fled their homes in search of food, amid the worst famine in the Horn of Africa in 60 years.
"It was horrible,” said Abdiqani Adan, who was there waiting for a food handout. “I heard gunshots as displaced people gathered for food distribution, several were killed and others injured."
The soldier started shooting after hungry crowds started pushing and shoving each other.
"A soldier killed five civilians and injured three others after he opened fire on people who had gathered for humanitarian assistance," said Somali security minister Mohamed Ali.
"It was a barbaric and unacceptable act, and government security forces swiftly arrested the perpetrator," he told Agence France Presse, adding that "investigations are still going on."
About 12.4 million people in the Horn of Africa region, including Ethiopia, Djibouti, Kenya and Uganda are suffering from the drought and need food aid, according to United Nations figures.
But Somalia, which has had no working government and has been dogged by violence for the past 20 years, is the hardest hit.
"More in terms of men and equipment will be required if we are to completely eliminate the extremist threat," said Somalia’s Prime Minister, Abdiweli Mohamed Ali. "The greatest need continues to be felt within areas still under the effective control of the extremists, where access by international humanitarian agencies is severely restricted," he added.
Three-quarters of a million Somalis, many of them children, now face death by starvation, according to the United Nations. Six regions in the south of the country have been officially classed as famine zones, including Mogadishu, where more than 100,000 people have arrived in the past two months in search of food.