First recall that $\operatorname{tr}(AB)=\operatorname{tr}(BA)$ and so $AB-BA$ has a null trace. In dimension $2$ the characteristic polynomial of a matrix $M$ is
$$
\chi_M(x) = x^2 - \operatorname{tr}(M)x + \det M
$$
with $\chi_M(M)=0$. Thus, in our case
$$
(AB-BA)^2 + \det(AB-BA)I = 0,
$$
which means that $(AB-BA)^2$ is a scalar matrix, i.e., it commutes with any other matrix $C$ (equivalently, it is in the center of $\mathbb R^{2\times 2}$).
Regarding the question in dimension $n>2$, we can use the fact that every matrix $Z$ with a null trace is in fact a commutator (see the Lemma below), i.e., there exist matrices $A$ and $B$ satisfying
$$
Z = AB-BA.
$$
Now take any matrix $M$ and consider the traceless matrix $Z=M-\lambda I$, where $\lambda=\operatorname{tr(M)}/n$. Then $Z$ is a commutator. However, should $Z^2=(M-\lambda I)^2$ be in the center of $\mathbb R^{n\times n}$, then $(M-\lambda I)^2=\alpha I$ for some $\alpha\in\mathbb R$, meaning that $M$ would be the root of a polynomial of degree $2$, namely $(x-\lambda)^2-\alpha$. But, for arbitrary $M$, this can only happen if $n\le2$.
For the sake of completeness, here is a self contained proof of the result used above [cf. Only Commutators Have Trace Zero & https://math.stackexchange.com/a/1140455/269050\]
Lemma
Let $\kappa$ be a field with $|\kappa|$ (possibly infinite) elements and $n<|\kappa|$. A matrix $Z\in\kappa^{n\times n}$ is tracelss if, and only if, it is a commutator
Proof. The if part is trivial. For the only if take a traceless matrix $Z$. Since the case $Z=0$ is trivial, we may assume that $Z$ is not scalar. Therefore, there exists some $\mathbf v\in\kappa^n$ that is not an eigenvector of $Z$. Thus $\mathbf v$ and $Z\mathbf v$ are linearly independent and there is a basis ${\cal B}=\{\mathbf v, Z\mathbf v, \dots\}$. In that basis $Z$ becomes
$$
[Z]_{\cal B}=\begin{bmatrix}
0 &\mathbf c^T\\
\mathbf e &\bar Z
\end{bmatrix},
$$
where $\mathbf e,\mathbf c\in\kappa^{n-1}$ and $\bar Z\in\kappa^{(n-1)\times(n-1)}$ (incidentally, $\mathbf e=(1,0,\dots,0)$). In particular, $\bar Z$ is traceless too. Hence, the induction hypothesis implies that $\bar Z$ is a commutator, say $\bar Z=AB-BA$. Let $\alpha\in\kappa$ be such that $A+\alpha I$ is invertible (to see that such an $\alpha$ exists think of $\det(A+\alpha I)$ as a monic polynomial in $\alpha$ and pick a value in $\kappa$ that is not a root, which is possible because $n<|\kappa|$). Since we can replace $A$ with $A+\alpha I$ and obtain the very same $Z$, we are entitled to assume that $A$ is invertible. Then
\begin{align*}
\begin{bmatrix}
0 &\mathbf 0^T\\
\mathbf0 &A
\end{bmatrix}
\begin{bmatrix}
0 &-\mathbf c^TA^{-1}\\
A^{-1}\mathbf e &B
\end{bmatrix}
&=
\begin{bmatrix}
0 &\mathbf 0^T\\
\mathbf e &AB
\end{bmatrix}\\
\begin{bmatrix}
0 &-\mathbf c^TA^{-1}\\
A^{-1}\mathbf e &B
\end{bmatrix}
\begin{bmatrix}
0 &\mathbf 0^T\\
\mathbf 0 &A
\end{bmatrix}
&=
\begin{bmatrix}
0 &-\mathbf c^T\\
\mathbf 0 &BA
\end{bmatrix}.
\end{align*}