ATHENS — Macedonia signed a landmark agreement on Sunday to change its name to North Macedonia, sealing a deal with Greece that would, if ratified, resolve a decades-old dispute and pave the way for the enlargement of the European Union and NATO.

Hailing a “patriotic and mutually beneficial agreement for both peoples,” Prime Minister Alexis Tsipras of Greece said it was important to see the accord through.

The accord must still pass a referendum in Macedonia and ratification in the parliaments of both countries.

“It is our historic responsibility to ensure that this step we are making is not left up in the air,” Mr. Tsipras said.