No. LetConsider the case in which $X$ beis the affine plane with coordinates $(s,t)$. Letdisjoint union of a closed subset of $Z$ beand the affine plane with coordinatesopen complement, and $(u,v)$$g:X\to Z$ is the obvious bijection. Let $Y$ be the affine line with coordinate $w$. Define $g$ by $(u,v)=(s,st)$$X$ and definelet $f$ by $w=st^2$be the identity map.