Both Mr Rajoelina and Mr Ravalomanana claim to be president
The leaders at the centre of Madagascar's political crisis have reached agreement on a power-sharing government, the UN special envoy says.
The accord would see a transitional period of 15 months, during which legislative and presidential elections would be held, Tiebile Drame said.
Ousted President Marc Ravalomanana said he would return to Madagascar but not personally take part in the process.
The deal follows days of negotiations in the Mozambican capital, Maputo.
Mediators hope it will bring an end to the months of crisis which culminated in the opposition leader, Andry Rajoelina, forcing Mr Ravalomanana to resign as president on 17 March and flee.
Mr Rajoelina and his allies, who accused the president of being a tyrant who misspent public money, were accused by the African Union of taking power through a coup and foreign aid was frozen.
The power struggle led to the deaths of more than 100 people in violent riots and crippled the island's tourist industry.
The leaders of Madagascar's four main political groups, including Mr Rajoelina, Mr Ravalomanana, and their predecessors as president, Didier Ratsiraka and Albert Zafy, began the power-sharing negotiations in Maputo on Wednesday.
The talks were mediated by the former Mozambican leader, Joaquim Chissano, on behalf of the Southern African Development Community (SADC).
Earlier, Mr Ravalomanana said that under the terms of the agreement, he would not take part in the transitional period, although his party would.
"In the interests of the nation, and following consultations, it seems reasonable to me to not participate personally," he said.
But he added that he would return to Madagascar, where he would be granted an amnesty from a conviction for abuse of power handed down in June. He was also fined $70m by the court in Antananarivo.
Mr Ravalomanana has been living in exile in South Africa since March. On Friday, Mr Rajoelina said any deal should not allow him to return.
An amnesty has also been agreed for Mr Ratsiraka, who has been living in exile in France since a crisis over the disputed results of the 2001 presidential election, which was won by Mr Ravolamanana.
He was sentenced to 10 years of hard labour and five years in jail in 2003 for misusing public funds and threatening state security.