Greek Foreign Minister Nikos Kotzias and his counterpart from the Former Yugoslav Republic of Macedonia (FYROM), Nikola Dimitrov, signed the deal on Sunday in the Greek village of Psarades, on the shores of Great Prespa Lake. The border between Greece and Macedonia lies in the water.

The controversial deal will see FYROM renamed as the Republic of North Macedonia. The accord should also allow Macedonia to join NATO and the European Union.

Sunday's ceremony was attended by the prime ministers of Greece and FYROM, Zoran Zaev and Alexis Tsipras. The EU's top diplomat, Federica Mogherini, was also at the signing.

The deal, which has been protested by hard-liners on both sides of the border, must now be ratified by the respective parliaments, and will also be put to a referendum in Macedonia. The process will take months.

Nearby protests

The area around Psarades where the ceremony was held was closed off to prevent protesters gaining access.

In the nearby Greek village of Pissoderi about 4,000 people held a rally in protest at the deal with banners proclaiming: "There is only one Macedonia and it is Greek."

Some marched to one of the police barricades and threw rocks. Several people were injured as police used tear gas against protesters gathered on a hillside about 30 kilometers (20 miles) from where the signing took place.

Macedonian media reported that 3,000 people had demonstrated against the accord in the southern city of Bitola.

In the Macedonian capital, Skopje, on Sunday night, grenades and tear gas were used by police to break up a protest rally outside the parliament building involving several hundred people.

A Reuters witness saw protesters pelting police with stones, chanting "Macedonia, Macedonia we will give our lives for Macedonia." Some of the demonstrators were arrested.

Read more: Macedonian President Ivanov says he won't sign 'disastrous' name deal with Greece

Greek nationalists protested again against the deal on Sunday

'Historic and necessary'

At the ceremony, Tsipras described the signing as a "brave, historic and necessary step for our peoples."

"We are here to heal the wounds of time, to open a path for peace, fraternization and growth for our countries, the Balkans and Europe," he said, adding that the time had come again "to sing happy songs in the Balkans."

Zaev called on the two countries to "step out of the past and look to the future," saying: "Our peoples want peace ... we will be partners and allies."

Historical dispute

The naming row between the two countries began 27 years ago when FYROM declared independence from the Socialist Federal Republic of Yugoslavia, but has roots going back to antiquity.

Athens has objected to its neighbor being called Macedonia because it has a northern province of the same name, the seat of Alexander the Great's ancient kingdom. Alexander the Great still represents a source of pride for many Greeks today.

Critics of the deal in Greece say the name change could imply territorial claims on the Greek province and usurp ancient Greek culture and civilization.

Opposition to the deal led to a no-confidence motion in parliament, with Tsipras surviving the vote on Saturday.

Nationalists in Macedonia, for their part, assert their country's right to bear the name without change.

The deal also means that Greece will lift its objections to the renamed nation joining the EU and NATO.

Read more: Macedonia: What's in a name?

tj, jm/cmk (Reuters, AFP, AP)

Each evening at 1830 UTC, DW's editors send out a selection of the day's hard news and quality feature journalism. You can sign up to receive it directly here.