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 NATO.

The foreign ministers of Greece and Macedonia signed an accord to rename the former Yugoslav republic the "Republic of North Macedonia," despite a storm of protest over a deal seen as a national sellout by some on both sides.

In the idyllic setting of Prespes, a lake region that borders Greece, Macedonia and Albania, officials from the two countries embraced, shook hands and penned a deal in the presence of European and United Nations officials.

The agreement still requires the approval of both parliaments and a referendum in Macedonia. That approval is far from assured, as it faces stiff opposition from the Greek public, and Macedonia's president has vowed to block the deal.

"We have a historic responsibility that this deal is not held in abeyance, and I am confident that we will manage it," Greek Prime Minister Alexis Tsipras said as he and his Macedonian counterpart Zoran Zaev received a standing ovation from guests at the lakeside ceremony.

Tsipras survived a no-confidence vote mounted by Greece's opposition in parliament on Saturday, but the depth of public emotion against the deal is strong.

In the Macedonian city of Bitola not far from the Greek border, protesters wrapped in national flags chanted "This is Macedonia" and began to march across the city.

"This shameful deal will not pass, we will defend Macedonia's name and pride," said Petre Filipovski, 40, dressed in the trademark red T-shirt of the opposition VMRO-DPMNE party.

In the Greek village of Pisoderi, protesters carried a giant Greek flag and one group clashed with riot police on a mountain slope as they tried to break the cordon. Others chanted the Greek national anthem.

Up to 70 percent of Greeks object to the compromise, an opinion poll by the Proto Thema newspaper showed on Saturday. In Psarades, the tiny lakeside community where the deal was signed, the village church bell tolled in mourning, draped in a Greek flag.

Others might object, too. "One big concern is Russia. Moscow has noticeably refused to endorse the agreement. It knows that this will see Macedonia join NATO," said James Ker-Lindsay, professor of politics and policy at St. Mary's University in London.

"There will be fears that Russia may now try to somehow influence the vote," Ker-Lindsay said. "Given recent allegations of Moscow’s involvement in other elections and referendums, this will be a real concern for NATO and the EU."

Under the deal, Greece will lift its objections to the renamed nation joining the EU and NATO.

"Our two countries have to turn from the past and look to the future," Zaev said. "We were bold enough to take a step forward."

Greece has been in dispute with Macedonia since 1991 over the former Yugoslav republic's name, arguing it could imply territorial claims over the Greek province of Macedonia and an appropriation of ancient Greek culture and civilization.

The subject is an emotional one for many Greeks. On Saturday, thousands of protesters outside the parliament building shouted "Traitor, traitor!" as lawmakers debated inside.

Veteran UN mediator Matthew Nimetz, who has been overseeing talks for a quarter century, described the agreement as an honorable deal. It was, he said, an example of "how neighbors can solve a problem if they really work at it".

"Today is my birthday," said Nimetz, who turned 79 on Sunday. "I told my family this year I don’t need any gifts because two prime ministers are going to give me a big gift."