He was killed in his vehicle near the presidential palace in the capital, Mogadishu, the officials added.
The president has cut short his visit to Ethiopia following Abas Abdullahi Sheikh's killing, state radio reports.
He grew up in a refugee camp, and became Somalia's youngest MP in November and a minister in February.
Somalia has been wracked by conflict since the long-serving ruler Siad Barre was ousted in 1991.
It is currently battling militant Islamists from the al-Shabab group, which is affiliated to al-Qaeda.
Security forces on patrol came across a vehicle blocking the road and, thinking it was being driven by militants, opened fire, police Major Nur Hussein told Reuters news agency.
Mr Abas was killed "by mistake - they opened fire on his car accidentally. May God rest his soul", Mogadishu mayoral spokesman Abdifatah Omar Halane was quoted as saying.
A rising star has been killed: Abdirizak Atosh, BBC Somali, Nairobi
Somali youth have been robbed of a role model following the shocking death of Mr Abas. Always polite and well-dressed, the former child refugee was widely admired for his determination to succeed.
Coming from a prominent religious family, Mr Abas studied at Kenya's prestigious Nairobi University and entered politics last year.
He became an MP for the port city of Kismayo in Somalia's Jubbaland region after an electoral college chose him ahead of a government minister who had been on the political scene for more than two decades.
Mr Abas' victory showed that Somalis were fed up with the old guard, and were demanding change. Sensing his popularity with the youth, the president appointed him to the cabinet in March.
Less than three months later, this rising star has become the latest casualty of more than two decades of violence in Somalia. But in this case some are asking: Has Somalia lost a future president?
Mr Abas was given a state funeral in Mogadishu attended by President Mohamed Abdullahi Mohamed Farmajo, members of parliament, Somali youth and elders.
President Farmajo has ordered security chiefs to "immediately get to the bottom of this unfortunate tragedy" and make sure the perpetrators were "brought to book".
Information Minister Abdirahman Osman said several people had been arrested, but did not give details, state-owned Radio Mogadishu reported.
Troops are sometimes nervous, and have previously shot officials - and each other - by mistake, says BBC Somalia analyst Abdullahi Abdi.
But Mr Abas, the public works minister, is probably the most high-ranking official killed by "friendly fire", he adds.
Mr Abas grew up in Dadaab camp in Kenya, home to hundreds of thousands of Somalis who fled drought and conflict.
The president appointed Mr Abas to the cabinet following his surprise election victory in February.
President Farmajo promised to improve security and establish an effective government in Somalia.
Much of the country is still under the control of al-Shabab.
Somalia's security forces have shot dead a 31-year-old government minister after mistaking him for a militant Islamist, officials have said.