* Al Qaeda-linked rebels abandon bases in Mogadishu
* Al Shabaab militants say their retreat only tactical
* Some say pullout may be driven by splits within al Shabaab
* Residents out in the streets; say too early to rejoice (Adds analyst)
By Mohamed Ahmed and Ibrahim Mohamed
MOGADISHU, Aug 6 (Reuters) - Somali President Sheikh Sharif Ahmed said on Saturday his military had defeated Islamist rebels battling to overthrow his Western-backed government after the al Shabaab group began withdrawing fighters from the capital Mogadishu.
Rejecting Ahmed's claim to have quashed al Shabaab's four-year insurgency, the militants' spokesman, Sheikh Ali Mohamud Rage, said their retreat was tactical only and they were holding their positions elsewhere in the anarchic country.
A 9,000-strong African peacekeeping force and Somali government forces had been steadily wresting control of rubble-strewn Mogadishu from the militants this year. Al Shabaab's pullout followed a string of fierce gun battles late on Friday.
Somalia has been without effective central government since the fall of dictator Mohamed Siad Barre 20 years ago.
Al Shabaab's retreat from the Somali capital Mogadishu signals an acceptance it cannot militarily defeat a government propped up by foreign muscle and firepower, but raises the spectre of an escalation in al Qaeda-inspired raids.
Winning Mogadishu might expand the government's prison capital a little, but it is unlikely to bring any tangible peace to the rest of the Horn of Africa country.
"It was not the strength of al Shabaab that kept them in Mogadishu for so long, it was the incompetence and weakness of the (Somali government)," said Afyare Elmi, a professor at Qatar University's International Affairs department.
"I'm worried the (government) may not be able to step into the vacated areas and other clan militia step in. The challenge ... is to expand into these areas and install law and order."
President Ahmed urged those who had fled their homes not to rush back to the city neighbourhoods now empty of militants until they had been cleared of explosives. The government said the rebels had retreated as far as 100 km (62 miles) from the capital.
"The Somali government welcomes the success attained by the Somali government forces backed by AMISOM who defeated the enemy of al Shabaab," Ahmed told a news conference at his residence.
Al Shabaab has never previously entirely left Mogadishu, raising questions over whether deep rifts among the al Qaeda-affiliated group's senior commanders had finally led to a split.
One faction prefers a more nationalist Somali agenda and wants to impose a harsh Islamic programme on the nation. Another more international wing aims to promote Jihad (holy war) and is bent on overthrowing a government they see as a Western stooge as well as forging closer ties with regional al Qaeda cells.
This faction had gained clout with the influx of foreign fighters. Those divisions became sharper as a series of military offensives in Mogadishu exacted a heavy toll on the rebels.
One rebel told Reuters the divisions had seen each side retreat from Mogadishu separately over the past few days, culminating in a massive exit by the rebels on Friday night.
"We understand there is disagreement among their top leaders," Farhan Ali, who lives in what was an al Shabaab controlled neighbourhood, told Reuters. "They were not displaced by force."
Similar sentiments were expressed by Somalis with links to al Shabaab in other parts of the country, including in the southern port city of Kismayu, the nerve centre of al Shabaab's operations in the south of the country.
"We have abandoned Mogadishu but we remain in other towns," Rage said on the al Shabaab-run Andalus radio station.
"We aren't leaving you, but we have changed our tactics. Everyone of you will feel the change in every corner and every street in Mogadishu. We will defend you and continue the fighting," Rage said.
Witnesses said convoys of al Shabaab "technicals" -- open-top 4x4s mounted with machine guns -- headed south from Mogadishu towards the al Shabaab-controlled town of Baidoa, 250 km southwest of the capital.
"I saw 50 armed al Shabaab vehicles heading towards Baidoa shortly after morning prayers," Aweys Sharif said by telephone from the town of Afgoye, 30 km south of Mogadishu.
Residents in Mogadishu's northern districts watched from the streets as the rebel fighters, many covering their faces with masks, left with the families.
"Today is a different day. We never dared watch them and their families like this. But still we are afraid to enter their bases. We cannot dance or rejoice yet," said Ali. (Additional reporting by Abdi Sheikh; Writing by Richard Lough; Editing by James Macharia and Sophie Hares)