You need to assume that $X$ is irreducible, otherwise, as Tom points out, there are easy counterexamples.
On the other hand, when $X$ is irreducible the answer is positive. The following proof is somewhat involved, it is quite likely that there is a more direct one.
One reduces immediately to the case $Y = \mathbb A^1$, and then the problem is to show that a regular function $f$ on $X$ that is constant along the fibers of $g$ descends to a regular function on $Z$.
Notice that it is enough to show that $f$ descends to a rational function $h$ on $Z$. In fact, if $X'$ is the normalization of $X$, we only need to show that $h$ has no poles along any closed irreducible subset $V$ of codimension $1$. If $W$ is an irreducible component of the inverse image of $V$ in $X'$, we obtain an extension $\mathcal O_{Z,V} \subseteq \mathcal O_{X,W}$ of DVRs; since the image of $h$ in $k(X')$ has no poles along $W$, it follows that $h$ is in $\mathcal O_{Z,V}$.
Consider the morphism $\phi \colon X \to Z \times \mathbb A^1$ induced by $f$ and $g$; its image $ \phi(X)$ contains a subset $U$ that is open in the closure $\overline{\phi(X)}$. The projection $U \to Z$ is dominant and injective; hence, by Zariski's main theorem it is an open embedding. The other projection $U \to \mathbb A^1$ gives the desired rational function on $Z$.