* Rebels agree to formation of unity government
* President Bozize vowed not to stand for re-election
* Mineral-rich country hit by poverty, conflicts
Central African Republic's government and rebels agreed on Friday to the formation of a national unity government under a ceasefire deal to end an insurgency that swept to within striking distance of the capital.
The agreement, signed in Gabon's coastal capital Libreville after three days of talks mediated by regional neighbours, averted the biggest threat to President Francois Bozize's decade in charge of the mineral-rich former French colony.
Aid groups had warned that a rebel attack on the capital Bangui could trigger a humanitarian crisis.
"God is great. He has spared us from the grave," Bozize told reporters after arriving at Bangui airport late on Friday. "I will finish my term, which ends in 2016."
Rebel spokesman Eric Massi said the deal was good for the country's conflict-weary citizens, but warned fighting could erupt again if the government failed to meet a list of rebel demands included in the accord.
Massi said that among the Seleka rebel coalition's demands was the release of political prisoners held by the government and the withdrawal of most of the foreign forces deployed to reinforce the country's military.
The deal also called for the transitional government to have a prime minister drawn from the opposition, and for a parliamentary election to be held within 12 months to replace the current national assembly.
"We will judge Mr. Bozize's sincerity in the coming days," Massi told Reuters by telephone.
Seleka, a coalition of five separate rebel groups, launched its insurgency in early December, accusing Bozize of reneging on a 2007 peace deal supposed to provide jobs and money to insurgents who laid down their weapons.
African countries - including Chad, Gabon, Cameroon, and Congo - deployed hundreds of troops to shore up Bozize's army after a string of defeats forced the army back to within 75 kilometers (45 miles) of Bangui.
The rebels had previously insisted that Bozize's resignation was a precondition for peace and that the president, who seized power in a Chadian-backed 2003 coup, should stand trial at the International Criminal Court.
Chad President Idriss Deby, who attended the signing ceremony, said the deal marked a good compromise.
"We have not undermined the integrity of the constitution of Central African Republic. President Bozize was elected for a five-year term and he should carry on until that is finished," Deby told reporters in Libreville.
U.N. special envoy to Central African Republic, Margaret Vogt, told the U.N. Security Council on Friday that the government and rebels needed to discuss why past peace deals had failed to avoid history repeating itself.
"We are hopeful that the agreements that were signed today in Libreville will contain the immediate flare-up and will calm the situation in CAR," Vogt told the 15-member council via videolink from Libreville.
"However, failure to go further to discuss the reasons for the lack of implementation of previous agreements and to correct these may lead to another meltdown, a few years down the line again, as a result of lost expectations and frustrations."
Central African Republic is one of a number of countries in the region where U.S. Special Forces are helping local soldiers hunt down the Lord's Resistance Army, an unrelated rebel group that has killed thousands of civilians across four nations.
The country remains one of the least developed on the planet despite rich deposits of gold, diamonds and uranium.
Vogt described the Central African Republic as an "aid orphan" and called for investment in peace and development to prevent the country from "falling down a slippery slope."
She said security was fundamental and that the success of the insurgency had been more a result of the failure of the government security forces than of the capacity of the rebels, adding that the army had "lost cohesion and the will to fight".
"Many of the soldiers simply dropped their weapons and melted into the bush," Vogt said.
"The international community now needs to engage more forcefully, both diplomatically and financially, to pull CAR from the brink," she said, adding that U.N. Secretary-General Ban Ki-moon had given the country the "same level of priority that he has accorded to Syria, Somalia and Mali".