Suppose that $(ab)^n=a^nb^n$ and $(ab)^{n+1}=a^{n+1}b^{n+1}$ for all $a,b\in G$.
If $(ab)^2=a^2b^2$ for all $a,b\in G$, then $G$ is abelian, since $abab=aabb$, so $ab=ba$. Thus if $n=1$ or $n=2$ then $G$ is abelian.
If $n\geq 3$, then let $G$ be a group of exponent dividing $n$, i.e., all elements of $G$ have order dividing $n$. Then $a^n=1$ for all $a\in G$, so the statement $(ab)^n=a^nb^n$ vacuously holds, and $(ab)^{n+1}=a^{n+1}b^{n+1}$ becomes $ab=ab$, and again vacuously holds.
The only issue is to construct a non-abelian group of exponent dividing $n$. If $4\mid n$ then the dihedral group of order $8$ will work. If $2p$ divides $n$ for some odd prime $p$ then the dihedral group of order $2p$ will work. Thus $n$ is odd, and the easiest example here is now the non-abelian group of order $p^3$ and exponent $p$, all upper unitriangular matrices of degree $3$ over $\mathbb F_p$. (Of course this example works for $n$ not a $2$-power as well, but dihedral groups are easier.)
(As an aside, we see why three consecutive numbers break this counterexample, as it becomes the case $n=1$ and $n=2$ again.)
Edit: Let's also show the three consecutive integers in a slightly more conceptual way. The two commutations conditions above yield
$$ a^{n+1}b^{n+1}=(ab)^{n+1}=(ab)^n ab=a^nb^nab.$$
Cancelling outer terms from the outer two equal expressions yields $ab^n=b^na$.
Thus for all $a,b\in G$, $a$ commutes with $b^n$. Now, if we also have $(ab)^{n+2}=a^{n+2}b^{n+2}$ then we also have that $a$ and $b^{n+1}$ commute, for all $a,b\in G$. In particular, $a$ commutes with $(b^{-1})^n$ and $b^{n+1}$, so it commutes with their product, $b$.