We will suppose that our spaces are locally connected, so that connected components are open and closed.
The space $Z$ can be covered by open connected subsets over which $q$ is trivial, and since the restriction of $res(p):p^{-1} (r^{-1}(U) = q^{-1}(U) \to r^{-1}(U)$ is still a covering , we may and will henceforth assume that $q$ is a trivial covering and that $Z$ is connected.
The core of the proof
Take a connected component $V\subset X$ of $X$ ( a sheet of the trivial covering $q$) .
Its image $p(V)$ will be a connected component of $Y$, according to Spanier's Algebraic Topology, Chap.3, Theorem 14, page 64.
But then $res(r):p(V)\to Z$ is a homeomorphism and since, by surjectivity of $p$, the space $Y$ is a disjoint union of such $p(V)$, the map $r:Y\to Z$ is a trivial covering whose sheets are exactly the connected components of $Y$.