European Union leaders have finally reached a highly conditional deal to help illegal immigrants who have fled across the Mediterranean in recent months.

The compromise deal will see the EU take 40,000 who have landed in Greece and Italy over the next two years - but mandatory quotas demanded by the European Commission have been rejected by a majority of EU states.

So far, Ireland has not agreed to accept any of these and no decision can be taken until a final number is proposed once the plan begins to come into place from July onwards.

Under the Schengen Agreement, Ireland has an automatic opt-out to any EU-wide immigration measure, but it retains the right to make exceptions in special cases.

A spokesman for the Irish Government said it would not be commenting until after the plan was finalised later on Friday.

The EU wants to oblige member countries to share 40,000 Syrians and Eritreans requiring international protection who are expected to arrive in Italy and Greece over the next two years. But many of the 28 nations are refusing to have migrant numbers dictated to them from Brussels.

Meanwhile, 20,000 Syrian refugees currently held in camps in Jordan and elsewhere will be given homes across the EU - though Ireland will take taking 572 of them, twice the number requested.

Eastern European countries and those from the Baltics, along with France, led opposition during a bitter row amongst EU leaders to Commission demands that mandatory quotas should be set by Brussels.

During heated exchanges, the Italian premier, Matteo Renzi reacted furiously in the face of opposition, telling colleagues: “If this is Europe, I want no part of it,” a series of sources said early today.

“It was a very intensive debate,” German chancellor Angela Merkel told reporters after the meeting ended, describing the migrant crisis “as the biggest challenge I have seen in European affairs in my time as chancellor”, ahead of the euro zone crisis.

Hungary, which has seen thousands of migrants cross its border by land and described the plan as absurd, and Bulgaria, one of the EU’s poorest states, have been granted exemptions.

Britain has opted out of the scheme, while nations in eastern Europe refused to accept set quotas.

This response prompted a furious reaction from Italy’s prime minister Matteo Renzi who told colleagues: “If this is Europe, I want no part of it,” a series of sources said early on Friday.

The Italians, the Greeks and the Maltese, given the pressures that they have faced in recent months, had led a push for mandatory figures to be met by the others.

Clearly doubtful that the plan can work, a visibly tired EU commission president Jean-Claude Juncker said he had wanted a mandatory agreement, but was prepared to work with what had been settled.

“I would have wanted that everyone accept a mandatory system but if the result is the same, why start a war,” he said 3am, complaining that negotiating in the early hours is ludicrous.

He described the plan as one of “modest ambition” and said at one point in the meeting he had told EU leaders “I don’t give a damn” about objections to the plan’s underlying methodology.

“We have to find out if the system works. It doesn’t matter if it is voluntary or mandatory, it is whether it can help 60,000 refugees,” Mr Juncker told a news conference in the early hours of Friday.

The Italian prime minister, Matteo Renzi was extremely angry during the meeting of EU leaders, banging his fists at one point on the table, saying: “If this is your idea of Europe, you can keep it.”

“If you don’t agree with 40,000 refugees you don’t deserve to be called Europe,” Mr Renzi was quoted as saying to fellow EU leaders during an emotional plea at a summit in Brussels. “If this is your idea of Europe, you can keep it.

“If we think Europe is only about budgets, it is not the Europe we thought of in 1957 in Rome,” Mr Renzi said, referring to the European Union’s founding treaty.

More than 114,000 migrants have been plucked from the Mediterranean trying to reach Europe so far this year, according to the International Organisation for Migration. Some 2,600 have died or gone missing during the often-perilous sea crossing.

The European Parliament president, Martin Schulz is equally doubtful that a voluntary agreement has any chance of success, saying before the 3am finish that they had been tried and failed in the past.

While Ireland has agreed to accept hundreds held in camps outside the EU, it has so far given no commitments to take any of those who have illegally entered the EU in recent months.

However, the Government can make no decision until the EU agrees the final numbers to be shared out from those who have already made their way across the Mediterranean.

Once that happens, a motion - if the Government then decides to take part - will be put to the Dail, as required under the legislation, sources told The Irish Times today.

Under the plan cobbled together in the early hours, the refugees held now in camps in the Middle East will be given a home across the EU over the next two years.

Speaking shortly before 3am in Brussels, the President of the European Council, Donald Tusk strongly emphasised that migrants without the right to claim refugee status will be sent back.

Paying tribute to actions in the past by Spain, when it shipped large numbers of illegal North African migrants back, Mr Tusk said EU rules must respected or the Schengen free-travel agreement “will be at risk”.