We are equipped with a function which takes three inputs $\mathrm{CDH}(h,h^a,h^b)$ that returns $h^{ab}$. We call it with the inputs $\mathrm{CDH}(g^x,g,g^{xy})$. If we write $a$ for the residue mod $p-1$ such that $ax\equiv 1\pmod{p-1}$ we see that if we define $h$ to be $g^x\mod p$ then $h^a=g^{ax}=g\mod p$ and $h^y=g^{xy}\mod p$. Thus for this choice of $h$ we have $\mathrm{CDH}(g^x,g,g^{xy})=\mathrm{CDH}(h,h^a,h^y)=h^{ay}=g^{axy}=g^y\mod p$.
There is a slight wrinkle when $x$ is not invertible mod $p-1$, for in this case $y$ is not uniquely defined by $g^{xy}$. To be precise, if $\mathrm{GCD}(x,p-1)=\ell$ then all of the values $y'=y+k\ell$ for $k=1,\ldots (p-1)/\ell$ would all have $g^{xy}=g^{xy'}\mod p$ so that $g^{y'}$ would be a legitimate answer fo any of the $y'$.
Our CDH oracle may be defined in such a way as not to accept $g$ as second argument in the case where $h=g^x$ and $x$ has a common factor with $p-1$, because $g$ does not lie in $\langle h\rangle$. In such cases we can take arbitrary $\ell$th roots of $g^x$ and $g^{xy}$ and use these as the second and third arguments and proceed as before but noting the multiple possible answers.
As an amusing aside, if we have the public values and shared secret for a Diffie-Hellman exchange, but do not know the generator (i.e. we know $g^x$, $g^y$ and $g^{xy}$ but not $g$), then such an oracle can recover $g$ since $\mathrm{CDH}(g^{xy},g^x,g^y)=g$.