What is the math behind this transformation on exponents that are logarithms?

By definition, $a=b^{\log_b(a)}$ and $n=b^{\log_b(n)}$. Therefore $$a^{\log_b(n)}=(b^{\log_b(a)})^{\log_b(n)}=b^{\log_b(a)\cdot\log_b(n)}=(b^{\log_b(n)})^{\log_b(a)}=n^{\log_b(a)}.$$