Rajoelina had repeatedly accused Ravalomanana of being a dictator and misusing public funds while Ravalomanana's supporters called Rajoelina a troublemaker bent on seizing power illegally.
The embattled president, whose palaces were surrounded by the military on Monday, announced that he was dissolving the government and handing presidential powers to a directorate of high-ranking military officers.
"After deep reflection, I have decided to dissolve the government and give up power so that a military directorate can be established,'' he said in a radio address.
"This decision was very difficult and very hard, but it had to be made. We need calm and peace to develop our country."
The military, in turn, handed power to Rajoelina, a 34-year-old former disc jockey and businessman who was dismissed last month by Ravalomanana as the mayor of the capital, Antananarivo.
"We give full powers to Mr Andry Rajoelina to become president of the high transitional authority," Hyppolite Ramaroson, a navy admiral and the most senior military official, told reporters on Tuesday.
Thousands of supporters cheered as Rajoelina travelled to the presidential office compound, which had been seized by troops on Monday.
"This is a David versus Goliath victory," one excited opposition supporter yelled.
'Road remains tough'
But Rajoelina warned that "we are now free but the road ahead remains rough".
|Ravalomanana's whereabouts were unclear after he stepped down under pressure [AFP]
At least 135 people have been killed since the country's political crisis began in January, most of them when security forces cracked down on anti-government protests at the order of Ravalomanana's government.
The crisis, with Rajoelina leading anti-government demonstrations since the start of the year, has also damaged the country's nearly $400m-a-year tourism sector.
Adding to the country's problems may be ostracism by the African Union, which had warned Madagascar's military against handing over power to Rajoelina.
"If the military hands over power to the mayor, it is not constitutional," Jean Ping, the African Union commission chairman, said before the military's decision was announced.
Ping said that any taking of power by non-constitutional means would be considered a coup d'etat by the AU and according to its charter, coups or unconstitutional changes of government are cause for automatic suspension.
Bruno Nongoma Zidouemba, Burkina Faso's ambassador to the 53-state AU, urged Rajoelina and the military to ensure Ravalomanana's safety.
"We have launched an appeal for law and order to prevail in Madagascar, we have also called for President Ravalomanana's security to be guaranteed, as well as that of his relatives and his entourage," he said.
The president's spokesman said that Ravalomanana, who had been sheltering at a presidential palace about 6km outside the capital, had been moved to an undisclosed location.
Robert Wood, a US state department spokesman, dismissed reports circulating in Madagascar that Ravalomanana had taken refuge in the US embassy in Antananarivo.