Macedonia's main political leaders reached a final agreement early Wednesday to resolve a protracted and severe political crisis that has roiled the small Balkan country for months.

The agreement reached in marathon European Union-mediated talks calls for early elections organized by a caretaker cabinet to be held April 24 and conservative Prime Minister Nikola Gruevski to step down by the end of this year.

Negotiations had failed twice in the past to produce results. The latest breakthrough came a day after U.S. Assistant Secretary of State Victoria Nuland urged Macedonia's political leaders to end the crisis that has divided society and seen the opposition boycott Parliament for months in a symbolic protest.

A special prosecutor will be named by September to investigate the opposition's claims that the government was behind the illegal wiretapping of 20,000 people, including police, judges, religious leaders, journalists and foreign diplomats.

Those claims are at the heart of the crisis, one of Macedonia's deepest since it gained independence from Yugoslavia in 1991. Main opposition Social Democrats leader Zoran Zaev has been publishing excerpts of the recorded conversations since February.

Many are between government officials and purport to reveal corruption at the highest level of government in this country of two million people, including mismanagement of funds, election fraud and spurious criminal prosecutions of opponents.

Zaev had long demanded Gruevski's resignation.

Gruevski has denied wrongdoing, claiming the recordings were made with the help of foreign spies and leaked to the opposition, and that the excerpts that were published were heavily edited. He has accused Zaev of plotting a coup.

European Union Enlargement Commissioner Johannes Hahn, who mediated the talks along with three other European Parliament members, praised the final deal signed by Gruevski, Zaev and two ethnic Albanian political leaders: Ali Ahmeti of the governing coalition's junior partner Democratic Union for Integration, and Menduh Thaci, who heads the opposition Democratic Party of Albanians.

"This is an excellent day for your country," Hahn told reporters early Wednesday after the 12 hours of talks ended.

Macedonia has long hoped to join both the EU and NATO.

In a separate press conference, Gruevski said the deal includes an end to the opposition's boycott, with it returning to Parliament in September. The opposition will also propose new ministers of interior, labor and social policy, as well as some deputy ministers.

"A political crisis in Macedonia is solved and the European perspective for the Republic of Macedonia remains open," Gruevski said, adding that the caretaker government organizing the 2016 election will be led by someone proposed by his conservative VMRO-DPMNE party.

In a victorious atmosphere in the Social Democrats headquarters, Zaev told about 100 supporters that the agreement backs democratic processes.

"We got Nikola Gruevski's resignation and he will not organize the elections," Zaev said.

Apart from the political crisis, Macedonia has also faced security issues this year, after a shootout between police and ethnic Albanian gunmen in the northern border town of Kumanovo in May left 18 people dead.

"The prolonged crisis has had grave consequences for the stability of the country, its international standing and its prospects for progress towards European Union membership," said European Parliament member Richard Howitt, who also monitored the talks.

"It is not an exaggeration to say the timing (of the) agreement could pull the country back from the brink."