Here $\mathbb{T}=\{z\in\mathbb{C}: |z|=1\}$ denotes the unit circle in the complex numbers.
This holds, if we have $\sigma(A)\subseteq \mathbb{R}$ or $\sigma(A)\subseteq \mathbb{T}$ (independent of $B$ in these cases), because in the first case, $A$ is self-adjoint and in the second case, $A$ is unitary and then one can check that $A(\text{ker}([A,B]))=\text{ker}([A^*,B])$, so the ranks are also equal.
If $\sigma(A)\subseteq \mathbb{R}\cup\mathbb{T}$ and $B$ is arbitrary, the statement does not hold in general, as this example shows, so one would have to use the assumption on $B$ too, if the statement was true.
One can also assume that one of the matrices is diagonal, because if $A=UDU^* $ and $B=VEV^* $, with diagonal matrices $D,E$ and unitary matrices $U,V$ then $$\begin{split}\text{rank}(AB-BA) &=\text{rank}(UDU^* VEV^* -VEV^* UDU^* )\\ &=\text{rank}(DU^* VEV^* U-U^* VEV^* UD)\\ &=\text{rank}(D(U^* V)E(U^* V)^*-(U^* V)E(U^* V)^* D)\end{split}$$ and $U^* V$ is just another unitary matrix.
A way to use the assumption on the spectrum is to write $A$ and $B$ as either the sum $A_1+A_2$ or the product $A_1A_2$ of a self-adjoint matrix $A_1$ and a unitary matrix $A_2$.
For the first version, one can split $D$ into the sum of two diagonal matrices, one with the real entries and $0$'s else, and one with the entries on the unit circle and $0$'s else, $D=D_1+D_2$. Then let $J$ be the diagonal matrix which has $1$'s where $D$ has real entries and $0$'s else, then $D=(D_1-J)+(D_2+J)$, so $A=U(D_1-J)U^*+U(D_2+J)U^*=:A_1+A_2$, where $A_1$ is self-adjoint and $A_2$ is unitary.
For the second version, one can split $D$ into the product of two diagonal matrices, one with the real entries and $1$'s where the entries from the unit circle were, and one with the entries from the unit circle and $1$'s where the real entries were, $D=D_1D_2$, which leads to $A=(UD_1U^*)(UD_2U^*)=:A_1A_2$ where $A_1$ is self-adjoint and $A_2$ is unitary again.
Note that in both cases, $A_1$ and $A_2$ commute.
I couldn't prove the assertion with this approach in the way one can prove it for unitary $A$ because $A^*A$ is not the identity in this case.
I also tried to find a counter-example using NumPy but this only gave me false examples due to rounding errors, e.g. $\begin{pmatrix}0 & 0 \\ 1 & 0\end{pmatrix}$ has rank $1$ but $\begin{pmatrix}0 & 10^{-16} \\ 1 & 0\end{pmatrix}$ has rank $2$.
It should also be noted that the dimension of the matrices can be assumed to be $>2$ because in the two dimensional case with $A=\begin{pmatrix}\lambda_1 & 0 \\ 0 &\lambda_2\end{pmatrix}$, $B=\begin{pmatrix}b_{11} & b_{12} \\ b_{21} & b_{22}\end{pmatrix}$ we have $[A,B]=\begin{pmatrix}0 & (\lambda_2-\lambda_1)b_{12} \\ (\lambda_1-\lambda_2)b_{21} & 0\end{pmatrix}$ and $[A^*,B]=\begin{pmatrix}0 & \overline{(\lambda_2-\lambda_1)}b_{12} \\ \overline{(\lambda_1-\lambda_2)}b_{21} & 0\end{pmatrix}$ which have the same rank (also independent of $B$).