MOGADISHU - Fighting erupted between Islamist rebels, government forces and African Union peacekeepers in Somalia's capital Mogadishu on Friday, killing at least 22 people, witnesses and medical staff said.

Battles broke out around the city's strategic K4 junction after the insurgents launched a pre-dawn attack on an AU base and on government troops. Witnesses said the clashes spread to three other districts and that most of the dead were civilians.

The toll was expected to increase as the fighting continued. Fearful residents cowered in their homes as mortar shells detonated around them and bullets tore into walls.

We have seen 17 dead people and taken 40 others to hospitals, senior ambulance official Ali Musa told Reuters.

A business leader in Mogadishu's sprawling Bakara Market said a further five people had died there when a mortar bomb exploded in a busy restaurant.

Western security agencies say Somalia, torn by civil war for 18 years, has become a haven for Islamist militants plotting attacks in the Horn of Africa and beyond.

Adding to the carnage, residents said Ethiopian troops also fought battles on the border in Bakool region with ethnic Somali rebels from the Ogaden National Liberation Front. Ethiopian officials could not immediately be reached for comment.

The international community wants to bolster the U.N.-backed government of President Sheikh Sharif Ahmed, which is fighting several rebel movements, including al Shabaab. The United States says that group is al Qaeda's proxy in Somalia.

Battles have been taking place across central and southern regions this week as pro-government militia try to seize towns from al Shabaab and another insurgent group, Hizbul Islam.

At least 33 people died at Bula Burde in the southern Hiran region on Thursday, and 12 more died when al Shabaab fighters drove pro-government gunmen out of Bulahawa.

That was just four days after the Ahlu Sunna Waljamaca militants seized that town from al Shabaab. Meanwhile, Hizbul Islam retook control of Luuq, another town in the Gedo region.

On Friday, residents said Ahlu Sunna fighters seized the central town of Mahas from al Shabaab after hours of clashes.

The rebels say Ethiopian soldiers are fighting alongside the pro-government militiamen inside Somalia. Ethiopia denies it.

Violence has killed more than 18,000 Somalis since the start of 2007 and driven another 1 million from their homes.

(Additional reporting by Ibrahim Mohamed and Mohamed Ahmed; Writing by Daniel Wallis; Editing by Alison Williams)