This is more or less a previous step in the proof of the famous theorem of Jacobson that states that a ring which satisfies $x^{n_x}=x$ for every element $x$ is commutative (see Structure theory for algebraic algebras of bounded degree (Jacobson, 1945; Theorem 11)).
First of all, $D$ has prime characteristic, since $2^n=2$ (this can not happen in characteristic $0$). Suppose $D$ is not commutative; then there exists $a\in D\setminus Z(D)$. The fundamental step is that there exists $b\in D$ such that $bab^{-1}=a^i$, with $a^i\neq a$ (see for example Lemma 3.1.1. of Noncommutative rings (Herstein, 1971 (2ed)). Then, if $P$ is the prime field of $D$, $P(a,b)$ is a finite ring because $a^n=a, b^n=b, a^ib=ba$, and it is a division ring because $x^{-1}=x^{n-2}\in P(x)$ for every $x\in D$. Therefore $P(a,b)$ is a finite division ring, that should be commutative by Wedderburn's little theorem, but isn't, as $ba=a^ib\neq ab$. This shows by contradiction that there is no such $a$, i.e., $D=Z(D)$ is commutative.
Now every element of $D$, which is a field, is a root of the polynomial $X^n-X$, so $D$ has at most $n$ elements.