If the second covariant derivative of every vector field $Z$ is symmetric in the sense that $\nabla(\nabla Z)$ (which is a section of $TM\otimes T^*M\otimes T^*M$) is a section of the sub bundle $TM\otimes S^2(T^*M)$, then the connection $\nabla$ is torsion-free and flat.

This follows directly from Cartan's structure equations (cf. Kobayashi and Nomizu). Here is the calculation. Let $\omega = (\omega^i)$ be a coframing on an open set in $M$ and let $\theta = (\theta^i_j)$ be the connection forms of $\nabla$ relative to the coframing $\omega$. The Cartan structure equation are
$$
\mathrm{d}\omega^i = -\theta^i_j\wedge\omega^j
+\tfrac12 T^i_{jk}\,\omega^j\wedge\omega^k
\quad\text{and}\quad
\mathrm{d}\theta^i_j = -\theta^i_k\wedge\theta^k_j
+ \tfrac12 R^i_{jkl}\,\omega^k\wedge\omega^l,
$$
where $T^i_{jk} = - T^i_{kj}$ and $R^i_{jkl} = -R^i_{jlk}$ are, respectively, the components of the torsion and curvature of $\nabla$
in the coframing $\omega$. (Note the use of the Einstein summation convention both here and below.)

If $Z$ is an arbitrary vector field in the domain of $\omega$, with $\omega$-components $z^i = \omega^i(Z)$, then there are functions $z^i_j$ and $z^i_{jk}$ that satisfy the equations
$$
\mathrm{d}z^i = -\theta^i_j\,z^j + z^i_j\,\omega^j
\quad\text{and}\quad
\mathrm{d}z^i_j = -\theta^i_k\,z^k_j + \theta^k_j\, z^i_k + z^i_{jk}\,\omega^k.
$$
Here, $z^i_j$ and $z^i_{jk}$ are the components of the first and second covariant derivatives of $Z$ relative to the coframing $\omega$.

Suppose that $z^i_{jk}=z^i_{kj}$ for every vector field $Z$. (This is the component version of the condition that $\nabla(\nabla Z)$ be a section of $TM\otimes S^2(T^*M)$.) Use the above structure equations and the symmetry assumption to compute
$$
0 = \mathrm{d}(\mathrm{d}z^i)
= \tfrac12\bigl(-R^i_{jkl}z^j
+ T^j_{kl}z^i_j\bigr)\,\omega^k\wedge\omega^l.
$$
Thus, $-R^i_{jkl}z^j + T^j_{kl}z^i_j = 0$ for every vector field $Z$. (I.e., the relation $R(X,Y)Z = \nabla_{T(X,Y)}Z$ holds for every triple of vector fields $X$, $Y$, and $Z$.) Since the value of $Z$ and its first covariant derivative can be specified arbitrarily at any point of $M$, this relation can only hold for all vector fields $Z$ if and only if $T^j_{kl}$ and $R^i_{jkl}$ vanish identically, i.e., the connection $\nabla$ is both torsion-free and flat.