Here is a solution that uses Jordan normal form. First, note that $XY-YX+I$ cannot be rank-one when, via the same similarity transform,
$$
X\sim\pmatrix{U_1&U_2\\ 0&U_3},\ Y\sim\pmatrix{V_1&V_2\\ 0&V_3}\tag{1}
$$
where $U_1,V_1$ are $r\times r$ for some $0<r<n$ and $U_3,V_3$ are $(n-r)\times(n-r)$. This is because $(1)$ implies that
$$
XY-YX+I\sim\pmatrix{U_1V_1-V_1U_1+I_r&\ast\\ 0&U_3V_3-V_3U_3+I_{n-r}},
$$
but the ranks of both $U_1V_1-V_1U_1+I_r$ and $U_3V_3-V_3U_3 +I_{n-r}$ are at least $1$ (because the two matrices have nonzero traces), so that
$$
\operatorname{rank}(XY-YX+I_n)\ge\operatorname{rank}(U_1V_1-V_1U_1+I_r)+\operatorname{rank}(U_3V_3-V_3U_3 +I_{n-r})\ge2.
$$
Now, if $Z$ has $k>1$ different eigenvalues $\lambda_1,\lambda_2,\ldots,\lambda_k$, by a change of basis over $\mathbb C$, we may assume that $Z$ is in Jordan form $Z=Z_1\oplus\cdots\oplus Z_k$, where each submatrix $Z_i$ is a Jordan form for the eigenvalue $\lambda_i$. Since $X$ and $Y$ commute with $Z$, they must assume block-diagonal forms $X=X_1\oplus\cdots\oplus X_k$ and $Y=Y_1\oplus\cdots\oplus Y_k$ where both $X_i$ and $Y_i$ have the same sizes as $Z_i$. But then $X$ and $Y$ will be in the form of $(1)$, which is a contradiction to the assumption that $\operatorname{rank}(XY-YX+I_n)$ has rank $1$.
Therefore $Z$ must possess a single eigenvalue $\lambda$ of multiplicity $n$. We may assume that $Z=J_{r_1}\oplus\cdots\oplus J_{r_m}$, where each $J_{r_i}$ denotes a Jordan block of size $r_i$ for the eigenvalue $\lambda$, with $1\le r_1\le\cdots\le r_m$ and $r_1+\cdots+r_m=n$.
Every matrix $B$ that commutes with such a Jordan form $Z$ can be partitioned into a block matrix form $(B_{ij})$, where each sub-block $B_{ij}$ is of the form
$$
\mathbb R^{r_i\times r_j}\ni
B_{ij}=
\begin{cases}
T_{ij}&\text{if }r_i=r_j\\
\pmatrix{T_{ij}\\ 0}&\text{if }r_i>r_j\\
\pmatrix{0&T_{ij}}&\text{if }r_i<r_j\\
\end{cases}\tag{2}
$$
and $T_{ij}\in\mathbb R^{\min(r_i,r_j)\times\min(r_i,r_j)}$ is an upper triangular (square) Toeplitz matrix. Since each $B_{ij}$ is "upper triangular", if we put $\mathcal I=\{1,\,1+r_1,\,1+r_1+r_2,\,\ldots,\,1+r_1+r_2+\cdots+r_{m-1}\}$ (i.e. each element of $\mathcal I$ is the row/column index of the top-left element of some sub-block $B_{ij}$ in $B$) and $\mathcal J$ be the complement of $\mathcal I$ in $\{1,2,\ldots,n\}$, then by $(2)$, $B([\mathcal I,\mathcal J],[\mathcal I,\mathcal J])$ will be in the form of $\pmatrix{W_1&W_2\\ 0&W_3}$, where $W_1$ is $m\times m$. Now, if $Z$ has any non-trivial Jordan block, then $m<n$ and hence both $W_1$ and $W_3$ are non-empty. This is true in particular for $X$ and $Y$. But then $(1)$ is true and we arrive at a contradiction. Hence $Z$ cannot have any non-trivial Jordan block, i.e. $Z$ must be a scalar matrix.