BERLIN—Germany’s top prosecutor Tuesday said two soldiers and an accomplice plotted high-profile assassinations that would be blamed on a migrant, escalating a controversy over right-wing extremism in the military that is weighing on the German government.

Police detained a 27-year-old soldier identified as Maximilian T. on suspicion of conspiring to kill high-ranking politicians and other prominent individuals whom he and his two alleged accomplices saw as proponents of a misguided refugee policy, the prosecutor said.

The targets included former German President Joachim Gauck and Justice Minister Heiko Maas, who have both favored accepting refugees, according to the prosecutor.

Maximilian T.’s alleged co-conspirators, identified as Franco A., 28, and Mathias F., 24, were both taken into custody late last month. German authorities said they uncovered the plot after the Austrian police earlier this year caught Franco A., a first lieutenant in the German military, trying to retrieve a handgun he had hidden in a bathroom at Vienna airport.

Investigators also found more than 1,000 rounds of ammunition stockpiled in the home of Mathias F., a student.