Suppose $M_1 = x_1$. Since $X = M_2 - M_1$ can be an arbitrary centred random variable, and the identity must hold for this, we can see that for every centred (and integrable etc.) random variable $X$, and real $x_1$, $$\mathbb{E}[f(X + x_1)] = f(x_1).$$
This clearly holds if $f$ is an affine function. We shall argue that it can hold only if $f$ is affine.
Working first in $\mathbb{R},$ suppose for the sake of contradiction that $f$ is not affine. Then there exist some $a < b < c$ such that $$ f(b)(c-a) \neq f(a)(c-b) + f(c)(b-a).$$ Take $x_1 = b$, and let $X = c-b $ with probability $(b-a)/(c-a)$ and $a-b$ with probability $(c-b)/(c-a)$. By direct computation, $$ \mathbb{E}[f(X+x_1)] = \frac{(c-b) f(a) + (b-a) f(c)}{c-a},$$ but by assumption this is not equal to $f(b) = f(x_1)$. It follows that $f$ must be affine everywhere.
The same argument works in $\mathbb{R}^d$ as well: $f$ is not affine iff there exist some $d$ linearly independent points $a_1,\cdots a_d,$ and a point $b = \sum \lambda_i a_i$ for $\lambda_i \ge 0, \sum \lambda_i = 1$, such that $f(b) \neq \sum \lambda_i f(a_i).$ Then again taking $x_1 = b,$ and $X = a_i - b$ with probability $\lambda_i$ serves as a witness violating the equality for nonaffine functions.