\documentclass[a4paper]{article}
\usepackage{german}
\usepackage[utf8]{inputenc}

\usepackage{pgfplots}

\begin{document}

\pgfdeclarelayer{background}

{
This is expected to be yellow:
\begin{tikzpicture}
%\tracingcommands=2\tracingmacros=2
	\pgfsetlayers{background,main}

	\draw node {Foreground};

	\pgfonlayer{background}
		\fill[yellow] (-1,-1) rectangle (1,1);
	\endpgfonlayer

\end{tikzpicture}
}

{
This as well:
\pgfsetlayers{background,main}
\begin{tikzpicture}
%\tracingcommands=2\tracingmacros=2

	\draw node {Foreground};

	\pgfonlayer{background}
		\fill[yellow] (-1,-1) rectangle (1,1);
	\endpgfonlayer

\end{tikzpicture}
}
\end{document}

