In fact, the following is a stronger condition:
Prop. Let $G$ be a group, and let $x$ and $y$ be elements of $G$. If $(xy)^2 = x^2y^2$ then $x$ commutes with $y$; that is, $xy=yx$.
If every element of the group has order dividing $2$, then both sides of the equation above are equal to $1$, so the equality holds for any two elements of $G$; if every element is its own inverse, then every element has order $1$ or $2$, then again both sides equal the identity, so the result follows for every pair of elements. If any pair of elements commute, then the group is abelian.
This is a stronger result, though, because it also applies in other cases, when neither side is the identity but they are nonetheless equal.
Proof. Write the equation explicitly as $xyxy = xxyy.$ Multiplying on the left by $x^{-1}$ and on the right by $y^{-1}$, We get $x^{-1}xyxyy^{-1} = x^{-1}xxyyy^{-1}$ which is the same as $yx = xy.$ So $x$ and $y$ commute. $\Box$
Now, try proving the following:
Proposition. Let $G$ be a group, and let $x$ and $y$ be two elements of $G$. The following are equivalent (that is, if one of them holds, then they all hold; if any on of them is false, then they are all false):
- $xy=yx$.
- $(xy)^2 = x^2y^2$.
- $(xy)^{-1} = x^{-1}y^{-1}$.
- There exists an integer $n$ such that $(xy)^{n+i} = x^{n+i}y^{n+i}$ holds for $i=0,1,2$.