However, al-Shabab described the move as a "change of military tactics".
The conflict has hampered aid efforts in the famine-hit country, with the militia barring some aid agencies from central and southern areas it controls.
African Union peacekeepers and government forces have for years been contained to small areas of the capital but have recently been gaining ground.
The pull-out followed reports of gun battles in the capital on Friday night.
Sheikh Ahmed told a press conference: "The Somali government welcomes the success attained by the Somali government forces backed by Amisom [peacekeepers] who defeated the enemy of al-Shabab."
However, a spokesman for the al-Qaeda-linked rebels, Ali Mohamed Rage, told a local radio station there would be no withdrawal from other regions of southern Somalia.
AnalysisIt seems very premature to be putting this down as a victory seeing that al-Shabab have vowed to fight on in Mogadishu but with different tactics.
They say they are going to switch to guerrilla tactics, so we could now see more suicide bombings, more grenade attacks and perhaps the use of more landmines.
These kind of triumphant noises were made at the end of 2006 when Islamists forces withdrew, only for them to come back a month later and cause chaos.
While al-Shabab says it's leaving, there are also some reports that they simply may be melting away in order to carry out these guerrilla-style tactics.
African Union peacekeepers aren't celebrating, they are being cautious. So it's not clear if it will be easier for the aid agencies to operate.
Very few analysts think peaceful days are ahead in Mogadishu.
A spokesman for the African Union peacekeepers in Somalia said the force was treating the rebel withdrawal with caution in case it was a trap.
Soldiers would not immediately deploy across Mogadishu, Lt Col Paddy Ankunda told the BBC.
Some analysts have suggested the Islamist insurgents withdrew because funding from the Arab world had dwindled and they had become militarily weaker.
Parts of the capital, where there are camps for displaced people, were last week among three areas newly declared by the United Nations to be suffering famine.
There are now a total of five famine zones in the country.
The UN says some 640,000 children are acutely malnourished in Somalia, while 3.2 million people are in need of immediate life-saving assistance.