4. Let the solution set of the equation $x^{2}-x-1=\left(x^{2}-1\right) \pi^{x}-$ $x \pi^{x^{2}-1}$ be $M$. Then the sum of the cubes of all elements in $M$ is ( ).
(A) 0
(B) 2
(C) 4
(D) 5
4.C.

Obviously, $x=0, \pm 1$ are all solutions to the original equation.
When $x \neq 0, \pm 1$, from
$$
\left(x^{2}-1\right)\left(\pi^{x}-1\right)=x\left(\pi^{x^{2}-1}-1\right)
$$

and $\pi$ being a transcendental number, we know $\pi^{x}-1=\pi^{x^{2}-1}-1$, which means $x=x^{2}-1$. At this point, there are two real solutions $x_{1}, x_{2}$.
From $x^{3}=x^{2}+x=2 x+1$, we know
$$
x_{1}^{3}+x_{2}^{3}=2\left(x_{1}+x_{2}\right)+2=4 \text {. }
$$

Therefore, the sum of the cubes of all elements in $M$ is
$$
0^{3}+1^{3}+(-1)^{3}+x_{1}^{3}+x_{2}^{3}=4 .
$$