BERLIN—Germany narrowly avoided a suspected Islamist attack this month after a 12-year-old boy twice planted an explosive device at a Christmas market that failed to detonate, German officials said Friday.

The failed plot is the latest of several to involve children in Germany, a trend that concerns authorities because minors are largely protected against surveillance by local law and generally face much lighter sentences than adults in courts.

The...