Yemeni President Ali Abdullah Saleh has signed a deal under which he will step down after months of unrest.
Mr Saleh signed the agreement, brokered by Yemen's Gulf Arab neighbours, in the Saudi capital, Riyadh.
Under the plan, he will transfer his powers to his deputy ahead of an early election and in return will get immunity from prosecution.
But protesters rallying in the Yemeni capital, Sanaa, said they would reject any deal giving the president immunity.
The demonstrators said the Gulf initiative ignored the "blood of martyrs", BBC Arabic correspondent Abdullah Ghorab in Sanaa reports.
A crackdown on anti-government protests, which began in February, has left hundreds of people dead and thousands wounded in Yemen.
The 69-year-old leader - who has ruled since 1978 - came close to signing the deal several times in the past, only to pull out at the last minute.
Meanwhile, clashes broke out between pro-Saleh troops and gunmen loyal to dissident chief Sheikh Sadiq al-Ahmar in Sanaa on Wednesday.
No casualties were immediately reported in the fighting in Sanaa's al-Hasaba district.
Mr Saleh signed the agreement in the presence of Saudi King Abdullah and other senior Saudi officials after flying to Riyadh on Wednesday morning.
Under the plan, the president will hand over power to deputy Abdrabuh Mansur Hadi in return for immunity from prosecution.
Mr Hadi is then expected to form a national unity government and also call for early presidential elections within 90 days.
The deal envisages that Mr Saleh will remain an honorary president for three months after signing the agreement.
In Riyadh, Mr Saleh pledged to co-operate with the new government which would include the opposition.
He also called on all Yemenis to be partners in rebuilding the conflict-torn country.
The breakthrough comes after intensive talks in Yemen by the UN envoy to the country, Jamal Benomar.
Treatment in New York
In June, Mr Saleh survived an attack on his compound in Sanaa and then flew to Saudi Arabia for medical treatment.
He returned to Yemen in September.
On Wednesday, he said he would go to New York to continue treatment.
UN Secretary General Ban Ki-moon said: "If he [Saleh] comes to New York, I'll be happy to meet him."
Mr Ban added that he was "encouraged by the positive development of the situation in Yemen".
What began as peaceful protests against President Saleh has increasingly degenerated into armed conflict involving different tribes and militias.
Five or six provinces are no longer under government control.
Mr Saleh - who had unified North and South Yemen in 1990 - had long argued that he was the only man who could control his politically and socially divided country, the BBC's Sebastian Usher says.
But in one of the poorest countries in the world, he was unable to provide the basic necessities for many Yemenis, who accused him of corruption and mismanagement, our correspondent adds.