Example 16 Let $f(x)=x^{2}+a x+b \cos x,\{x \mid f(x)=0, x \in \mathbf{R}\}=\{x \mid$ $f(f(x))=0, x \in \mathbf{R}\} \neq \varnothing$, find all real values of $a, b$ that satisfy the condition.
Let $x_{0} \in\{x \mid f(x)=0, x \in \mathbf{R}\}$, then $b=f(0)=f\left(f\left(x_{0}\right)\right)=0$.
Thus, $f(x)=x(x+a)$. Therefore,
$$
f(f(x))=f(x)(f(x)+a)=x(x+a)\left(x^{2}+a x+a\right) .
$$

Obviously, $a=0$ satisfies the condition.
If $a \neq 0$, since the roots of $x^{2}+a x+a=0$ cannot be 0 or $-a$, the equation $x^{2}+a x+a=0$ has no real roots, hence $\Delta=a^{2}-4 a<0$, so $0<a<4$.
In conclusion, the values of $a, b$ that satisfy the given conditions are: $0 \leqslant a<4, b=0$.