Let's take the following heuristic argument. There are perhaps ways to prove that the guesses we make are necessary. For instance, since the coefficients on the right-hand side decay as $1/n^2$, the series converges uniformly to $f(x)$, and this for instance justifies the use of the formula for the Fourier coefficients below, because that allows us to switch the integral and the infinite sum. But that's as far as I'll go with that (perhaps someone can fill in this rigorous details if it's possible).
We assume (without really knowing; our assumption will be justified at the end) that $f$ is as many times differentiable as is necessary. The series for $f(x)$ is a Fourier series, and so we know that the coefficients can be computed as
$$
\frac{(-1)^k}{(\pi k)^2}=\int_{-1}^1\cos(\pi k x)f(x)dx.
$$
Integrating by parts three times yields
\begin{align}
\frac{(-1)^k}{(\pi k)^2}
&=\frac{1}{(\pi k)^3}\int_{-1}^1\sin(\pi k x)f^{(3)}(x)dx
+\frac{(-1)^k}{(\pi k)^2}(f'(1)-f'(-1)).
\end{align}
Staring at this lovingly, we can see that we can automatically satisfy this equation for all $k$ if we guess that
$$
f^{(3)}(x)=0,
$$
and
$$
f'(1)-f'(-1)=1,
$$
and this is satisfied by the function
$$
\tilde{f}(x)=\frac{1}{4}x^2+bx +c.
$$
To get $c$, it is enough to evaluate the Fourier coefficient for $k=0$. To get $b$, we note that $f$ must be even, since it is a sum of cosines, and so $b=0$.