BERLIN—German prosecutors have opened an investigation against a television comedian on suspicion of offending Turkish President Recep Tayyip Erdogan, escalating a dispute over press freedom that comes as Germany relies increasingly on Turkey to solve Europe’s migrant crisis.

The prosecutor’s office in the city of Mainz is launching proceedings on suspicion that prominent German comedy host Jan Böhmermann breached a law that prohibits offending foreign heads of state or members of government, senior public prosecutor Andrea Keller said.

Under paragraph 103 of the German criminal code, offending a foreign head of state can be punishable by up to three years in prison.

The legal probe comes two days after German Chancellor Angela Merkel criticized Mr. Böhmermann’s satirical poem, which made crude sexual jokes about Mr. Erdogan. It also follows a weekslong spat over German media criticism of Mr. Erdogan and Turkish reaction to it.

Ms. Merkel’s spokesman, Steffen Seibert, said the chancellor and Turkish Prime Minister Ahmet Davutoglu agreed in a phone call on Sunday evening that the text of the poem that aired on public broadcaster ZDF last Friday was “deliberately offensive,” voicing a rare official media criticism in a country that strongly advocates freedom of the press.