BERLIN—Turkey formally requested that Germany investigate a television sketch that ridiculed the Turkish president, setting up a battle that pits relations between the two countries against Germany’s freedom-of-expression laws.

The Turkish government has expressed outrage over a poem recited by German comedy host Jan Böhmermann that made crude sexual jokes about President Recep Tayyip Erdogan. It comes at a critical time in German-Turkish relations, as Berlin needs Mr. Erdogan’s help to stem the flow of migrants streaming...