*(I never know which one is necessity and which one is sufficiency, so I'm doing both)*

If $AX-XA=B$, then
$$
\begin{bmatrix}I&X\\0&I\end{bmatrix}^{-1}\begin{bmatrix}A&0\\0&A\end{bmatrix}\begin{bmatrix}I&X\\0&I\end{bmatrix}= \begin{bmatrix}I&-X\\0&I\end{bmatrix}\begin{bmatrix}A&0\\0&A\end{bmatrix}\begin{bmatrix}I&X\\0&I\end{bmatrix}=\begin{bmatrix}A&B\\0&A\end{bmatrix}.
$$
$$
\
$$

Conversely, suppose that $\begin{bmatrix}A&0\\0&A\end{bmatrix}$ and $\begin{bmatrix}A&B\\0&A\end{bmatrix}$ are similar. Then there exists $\begin{bmatrix}P&Q\\R&S\end{bmatrix}$, invertible, with
$$
\begin{bmatrix}P&Q\\R&S\end{bmatrix}\begin{bmatrix}A&0\\0&A\end{bmatrix}=\begin{bmatrix}A&B\\0&A\end{bmatrix}\begin{bmatrix}P&Q\\R&S\end{bmatrix}.
$$
This gives us
$$\tag1
AP-PA=-BR,
$$
$$\tag2
AQ-QA=-BS,
$$
and
$$\tag3
RA=AR,\qquad SA=AS.
$$
Now,

if $R$ is invertible, by $(1)$ we can take $X=-PR^{-1}$ (note that $R^{-1}$ commutes with $A$ by $(3)$);

similarly, if $S$ is invertible by $(2)$ we can take $X=-QS^{-1}$;

if $R+S$ is invertible, adding $(1)$ and $(2)$ we see that we can take $X=-(P+Q)(R+S)^{-1}$;

and if $R-S$ is invertible, subtracting $(2)$ from $(1)$ we see that we can take $X=-(P-Q)(R-S)^{-1}$.

In summary, if at least one of $R,S,R+S,R-S$ is invertible, we are done.

At this stage I was convinced that the invertibility of $\begin{bmatrix} P&Q\\ R&S\end{bmatrix}$ would force at least one of $R,S,R+S,R-S$ to be invertible. But I haven't found an argument yet. A few attempts looking for counterexamples when none of $R,S,R+S,R-S$ is invertible did not bear fruit, because the examples I had in hand forced $A$ and $B$ to be trivial; this could be just a feature of my choice of examples, though, as naturally I was working with rather small matrices.