The signing ceremony was protected by a heavy police presence on both sides of the border as opponents of the agreement in both countries had announced protests.
A crowd raised a banner saying "Macedonia is Greek" and tried to break a police cordon. We want to be Macedonians. A few flame bombs were thrown by police and one protester was injured in the leg and taken away by ambulance. This should remove obstacles to the country's entry into the European Union and North Atlantic Treaty Organisation.
The Macedonian parliament is scheduled to start debating the agreement next week.
Greek Prime Minister Alexis Tsipras hailed the agreement on Sunday, saying: "This is a fearless, historic and necessary step for our peoples".
The agreement follows decades of inconclusive talks which had soured relations between the two countries. "By signing the agreement. we have really moved mountains".
"So that we can heal the wounds of the past, open a path for peace, friendship, cooperation and growth, not only for our countries, but for all Balkans and Europe", he said as he and his FYROM counterpart Zoran Zaev received a standing ovation from officials at the ceremony.
We have a historic responsibility that this deal is not held in abeyance.The UN representative for the naming dispute, Matthew Nimetz and the European Commissioner for European Neighbourhood Policy and Enlargement Negotiations, Johannes Hahn, also attended the signing.
Nimetz, who turned 79 on Sunday, and was given a birthday cake, has been trying to broker a solution since 1994, first as a U.S. envoy and subsequently on behalf of the United Nations.
It was the election of Zaev in 2017, an economist, that proved crucial to the agreement which could still unravel however. Talks with Greece resumed after Zaev took office in the Balkan state of 2 million past year and made solving the quarrel a priority.
Taxi runs into crowd in central Moscow, 7 injured
A video taken by a security camera shows the moment when the taxi moves to the sidewalk and hits the pedestrians. Police said they have detained the driver, saying the person apparently lost control of the vehicle.
The Macedonian delegation, led by Prime Minister Zoran Zaev, arrived from Macedonia by boat across Lake Prespa.
Greece argued the term "Macedonia" implied territorial claims on its province of the same name, which is the birthplace of the ancient warrior king Alexander the Great, and usurped its ancient Greek heritage and history.
Ending a dispute that began 27 years ago, but the roots of which date back centuries, Greece's Nikos Kotzias and Nikola Dimitrov of Macedonia will meet at the Prespes Lake district to sign the historic accord, brokered after months of diplomatic haggling.
Greece and Macedonia defied protests and set aside three decades of dispute on Sunday as they agreed on a new name for the Balkan state, potentially paving the way for Macedonia's admission to the European Union and North Atlantic Treaty Organisation. Macedonian President Gjorge Ivanov said he would use his veto to block the deal, according to media reports. He added that it was a "mutually-beneficial agreement" for both sides.
The deal still needs to be approved by Macedonia's parliament and confirmed in a Macedonian referendum.
The deal will finally have to be ratified by the Greek parliament, a process which may also not be straightforward.
The party is a vociferous critic of the name deal.
Greece also has a region called Macedonia.