5. Find all functions $f: \mathbf{R} \rightarrow \mathbf{R}$ such that
$$
f\left(x^{2}+f(y)\right)=f(f(x))+f\left(y^{2}\right)+2 f(x y)
$$

for all $x, y \in \mathbf{R}$.
5. First, prove several properties of $f$.

Property 1: $f(0)=0$.
Proof of Property 1: In the original equation, set $x=y=0$ to get the result.

Property 2: For any $x \in \mathbf{R}$, $f\left(x^{2}\right)=f(f(x))$.
Proof of Property 2: In the original equation, set $y=0$ to get
$f\left(x^{2}+f(0)\right)=f(f(x))+3 f(0)$.
Then use Property 1 to obtain Property 2.

Property 3: If real numbers $a, b$ satisfy $f(a)=f(b)$, then for any $x \in \mathbf{R}$, $f(a x)=f(b x)$.
Proof of Property 3: In the original equation, set
$$
\begin{array}{l}
y=a \text { and } y=b \text { to get } \\
\quad f\left(x^{2}+f(a)\right)=f(f(x))+f\left(a^{2}\right)+2 f(a x), \\
f\left(x^{2}+f(b)\right)=f(f(x))+f\left(b^{2}\right)+2 f(b x) .
\end{array}
$$

Notice that the left-hand sides of the above two equations are equal. By Property 2, we have
$$
f\left(a^{2}\right)=f(f(a))=f(f(b))=f\left(b^{2}\right) \text {. }
$$

Thus, $f(a x)=f(b x)$. Property 3 is proved.
If there exists $a \neq 0$ such that $f(a)=0$, then by Property 3, for any $x \in \mathbf{R}$, we have
$$
f(x)=f\left(a \cdot \frac{x}{a}\right)=f\left(0 \cdot \frac{x}{a}\right)=f(0)=0 .
$$

Therefore, $f(x) \equiv 0$, which is easily verified to be a solution to the original equation.
From now on, assume $f(a) \neq 0$ for $a \neq 0$.
Using Property 2, the original equation can be rewritten as
$$
f\left(x^{2}+f(y)\right)=f\left(x^{2}\right)+f\left(y^{2}\right)+2 f(x y) \text {. }
$$

Clearly, the right-hand side of the above equation is symmetric in $x$ and $y$. Therefore, the left-hand side is also symmetric in $x$ and $y$, i.e., for any $x, y \in \mathbf{R}$, we have
$$
\begin{array}{l}
f\left(y^{2}+f(x)\right)=f\left(x^{2}+f(y)\right) \\
=f\left(x^{2}\right)+f\left(y^{2}\right)+2 f(x y) .
\end{array}
$$

Next, we prove: $f(x)>0$ for $x \neq 0$.
If not, then there exist non-zero real numbers $s, t$ such that $f(s)=-t^{2}$. In equation (1), set $x=s, y=t$ to get
$$
f\left(s^{2}+f(t)\right)=f\left(t^{2}+f(s)\right)=f(0)=0 .
$$

Thus, $f(t)=-s^{2}$.
In equation (1), set $x=\sqrt{s^{2}+t^{2}}, y=s$, to get
$f\left(s^{2}+t^{2}+f(s)\right)$
$$
=f\left(s^{2}+t^{2}\right)+f\left(s^{2}\right)+2 f\left(s \sqrt{s^{2}+t^{2}}\right) \text {, }
$$

i.e., $f\left(s^{2}+t^{2}\right)+2 f\left(s \sqrt{s^{2}+t^{2}}\right)=0$.
Similarly, in equation (1), set $x=\sqrt{s^{2}+t^{2}}, y=t$ to get
$$
f\left(s^{2}+t^{2}\right)+2 f\left(t \sqrt{s^{2}+t^{2}}\right)=0 \text {. }
$$

Therefore, $f\left(s \sqrt{s^{2}+t^{2}}\right)=f\left(t \sqrt{s^{2}+t^{2}}\right)$.
By Property 3, we have $f(s)=f(t)=-s^{2}$.
But in equation (1), set $x=y=s$ to get
$$
\begin{aligned}
0 & =f\left(s^{2}+f(s)\right) \\
& =f\left(s^{2}\right)+f\left(s^{2}\right)+2 f\left(s^{2}\right)=4 f\left(s^{2}\right),
\end{aligned}
$$

which contradicts $s \neq 0$.
Therefore, for any $x \neq 0$, $f(x)>0$.
Next, we prove: $f$ is injective on the interval $(0,+\infty)$.
If not, then there exist $a>b>0$ such that $f(a)=f(b)$. By repeatedly using Property 3, we have
$$
f\left(\left(\frac{a}{b}\right)^{n}\right)=f\left(\left(\frac{a}{b}\right)^{n-1}\right)=\cdots=f\left(\frac{a}{b}\right)=f(1)
$$

for any positive integer $n$.
Choose a positive integer $n$ such that $\left(\frac{a}{b}\right)^{n}>f(1)$.
In equation (1), set $x=\sqrt{\left(\frac{a}{b}\right)^{n}-f(1)}, y=1$, to get
$$
\begin{array}{l} 
f\left(\left(\frac{a}{b}\right)^{n}\right)=f\left(\left(\frac{a}{b}\right)^{n}-f(1)\right)+f(1)+ \\
2 f\left(\sqrt{\left(\frac{a}{b}\right)^{n}-f(1)}\right) .
\end{array}
$$

But $f\left(\left(\frac{a}{b}\right)^{n}\right)=f(1)$, and $\left(\frac{a}{b}\right)^{n}>f(1)$, indicating that the first and third terms on the right-hand side of equation (2) are both greater than 0, leading to a contradiction. Therefore, $f$ is injective on the interval $(0,+\infty)$.

Using the injectivity of $f$ on the interval $(0,+\infty)$, for any $x \neq 0$, from $f(f(x))=f\left(x^{2}\right)$, we get
$f(x)=x^{2}$.
By Property 1, the above equation also holds for $x=0$.
It is easily verified that $f(x)=x^{2}$ is a solution to the original equation. In summary, the functions that satisfy the conditions are $f(x)=0$ and $f(x)=x^{2}$.
(Provided by Xiong Bin, translated by Li Long)