German Chancellor Angela Merkel says her government has granted a Turkish request to allow the possible prosecution of a TV comedian who wrote a crude poem about Turkey's president.

Ankara has demanded to have German comedian Jan Boehmermann prosecuted for insulting a foreign head of state, which Ms Merkel's government had to grant permission for under German law.

The decision leaves it to prosecutors to decide whether they file charges.

Boehmermann read the poem on ZDF television two weeks ago to illustrate what he said would not be allowed in Germany, contrasting it with another channel's earlier satirical song that also poked fun at Turkish President Recep Tayyip Erdogan.

ZDF withdrew the passage from its archives but argues that it did not break the law.

Ms Merkel stressed that the decision "means neither a pre-judgment of the person affected nor a decision about the limits of freedom of art, the press and opinion". She underlined the independence of the judiciary and the presumption of innocence.

She also said the government intends to repeal the law on insulting a head of state, effective in 2018.

The request posed an awkward choice for Germany at a time when Ms Merkel is relying on Turkey to reduce the influx of migrants to Europe.

Officials spent days mulling Turkey's demand, and Ms Merkel acknowledged there had been internal divisions.

While permission to prosecute was granted in this case, she said the government agreed that the law allowing prosecution for insulting a foreign head of state is "dispensable in the future".

Boehmermann's poem started by describing the Turkish leader as "stupid, cowardly and uptight" before descending into crude sexual references.

While the German government defended the earlier satirical song as legitimate free speech, it distanced itself from the poem, saying last week that it was "deliberately offensive".

German officials have appeared at pains to avoid causing further friction with Mr Erdogan, steering clear of direct criticism of the president in recent weeks amid Turkey's sharp response to German satire.

Ms Merkel's governing coalition was divided over the Turkish request. The Social Democrats, her junior coalition partners, had advocated rejecting it.

Justice minister Heiko Maas, a Social Democrat, said this was the first such case in which the statement at stake was made by a journalist in a satirical programme. "Freedom of opinion, the press and art are things requiring the highest protection under our constitution," he said.

"The idea of lese-majeste no longer has a place in our criminal law," Mr Maas said.

The German Federation of Journalists said Ms Merkel's announcement sent "the wrong signal to the Turkish government".

Germany's criminal code provides for up to three years in prison for insulting a foreign head of state. However, Alexander Thiele, a legal expert at the University of Goettingen, told n-tv television that even if Boehmermann is convicted, "one can assume that he faces a small fine at most".

In addition to the request to have Boehmermann prosecuted for insulting a foreign head of state, Mr Erdogan also has filed a criminal complaint against Boehmermann under a separate law, alleging slander.

Prosecutors in the western city of Mainz, where ZDF is based, are already examining that complaint.