aggfunc=pd.Series.nunique
provides distinct count.
Full Code:
df2.pivot_table(values='X', rows='Y', cols='Z',
aggfunc=pd.Series.nunique)
Credit to @hume for this solution (see comment under the accepted answer). Adding as an answer here for better discoverability.