An agreement has been reached between German Chancellor Angela Merkel and Interior Minister Horst Seehofer, defusing a row over migration that had threatened her government.

Mr Seehofer, the leader of the Bavarian Christian Social Union (CSU), said the CSU and Mrs Merkel's Christian Democrats (CDU) had come to a clear solution to stop illegal immigration at the German-Austrian border after several hours of talks in Berlin.

He added that he intended to stay on in his cabinet post after earlier threatening to quit.

Mrs Merkel said that Germany would hold migrants registered in other European Union countries in transit centres while it negotiates their return.

She said the compromise between the CSU and the CDU would secure the principle of freedom of movement within the EU while allowing Germany to take "national measures" to limit migrant arrivals.

"We want on the one hand to set up transit centres in Germany and from there carry out returns in agreement with countries from which asylum seekers come and where they are already registered," Mrs Merkel told reporters.

"As such the spirit of partnership in the European Union is preserved and at the same time an important step to order and control secondary migration and that's why I think that we have found a good compromise after tough negotiations and difficult days."

But the crisis, the latest in a series of rows over migration between the two parties, is another sign of the EU-wide divide between those who want to maintain open borders and those who want to restrict the number of migrants entering the bloc.

Mrs Merkel's CDU relies on the CSU to maintain power through a coalition, which also includes the centre-left Social Democrats.

The CSU faces a strong challenge from the far-right Alternative for Germany in October's regional election.

Even if the immigration dispute has now been settled, it leaves Mr Seehofer, who has gone back on a threat to resign, diminished, experts said.

The dispute, and the compromise, are also a sign that the authority of Mrs Merkel is waning after more than 12 years in office.