The part that does not work in general is finding $r>1$ so that $a^r=a \mod b$, as Thomas mentioned. This works as long as $\gcd(a,b)=1$. Then $a$ is in the unit group modulo $b$, i.e. $a \in (\mathbb{Z}/b\mathbb{Z})^\times$. So it has some order $k$ so that $a^k=1$, then $a^{k+1}=a$.
Now if $gcd(a,b) \neq 1$, we can show that for any common prime factor $p$, the higher power of $p$ dividing $a$ is a power of $p^6$. T
Using the Chinese Remainder Theorem, I can assume $b=p^k$ for some $k\geq 1$. $p|a$ so $a=0 \mod p$.
We know $a=x^2=y^3 \mod b$ (I changed p,q to x,y). The next few lines just show that the largest power of $p$ diving $a,x^2,y^3$ are all the same, assuming $p^k$ does not divide $a$.
Since $b=p^k$, we reduce modulo $p$ to get $a=x^2=y^3=0 \mod p$.
So $p|x$ and $p|y$.
Write $x=up^m,y=vp^n$ where $p$ does not divide $u$ and $v$.
Then $x^2=y^3 \mod p^k$ so $u^2p^{2m}=v^3p^{3n} \mod p^k$.
If $a=0 \mod p^k$, e get your other case, $b|a$.
Assume $a \neq 0 \mod p^k$ so that $2m,3n<k$.
So $2|m$ and $3|n$, i.e. $6|2m=3n$.
Writing $a=wp^s$ as we did for $x,y$, we see the powers of $p$ are the same for $a,x^2,y^3$ since they are all equal modulo $p^k$.
Therefore $6|s$ and we can write $a=w(p^t)^6$.
$\gcd(w,b=p^k)=1$ and $w=u^2=v^3$ (check this) so it is a sixth power by your argument. So altogether, $a$ is a sixth power modulo $b$.
I realized afterwards, I am just using the fact that $\mathbb{Z}/b\mathbb{Z}$ is a UFD. Afterwards, the proof is just like for $\mathbb{Z}$. So this is really a fact about UFDs with a finite unit group.