This is because $Z$ is a complex subvariety of $X$. I think the easiest way to see it is to use local coordinates. First assume $Z$ is smooth to simplify things a little and let $p = \dim_{\mathbb C} Z$.
Let $u$ be some closed $2p$-form on $X$ and let $x$ be a point on $Z$. We can find holomorphic local coordinates $(z_1,\dots,z_n)$ on $X$ such that $Z$ is described by the set $\{ z \mid z_{p+1} = \dots = z_n = 0\}$ in these coordinates. Now write $u = \sum_{a+b = 2p} u_{ab}$ as a decomposition of $(a,b)$-forms. Locally, we have $ u_{ab} = \sum_{I,J} u_{ab,IJ} d z_I \wedge d\overline z_J, $ where $I,J$ are multi-indices such that $|I| = a$ and $|J| = b$. But now linear algebra shows that for $dz_I$ to be nonzero once restriced to $Z$, we must have $a = |I| \leq p$. Similarly, we get $b = |J| \leq p$, so $a = b = p$.
For non-smooth $Z$, one can show that the integral over $Z$ of $u$ is equal to the integral over the nonsingular part of $Z$ only. This takes a little work, but is done in one of the first chapters of Demailly's book "Complex analytic and differential geometry". Since the nonsingular part is dense in $Z$ this implies the result.
Now, if $Z$ is just an arbitrary closed smooth subvariety of $X$, there is no reason that it should define a $(p,p)$-class, and indeed it doesn't in general.