It isn't obvious, but this matrix was constructed so that its eigenvalues are 1, 2, and 3. Let's see this with the matrix A = gallery(3) If we deliberately violate this condition by taking B to be -A', we find that the computed solution blows up. The equation has a unique solution when the eigenvalues of A and -B are distinct. Near the end of the documentation for sylvester is this single sentence: The help entry for sylvester does not mention any conditions, other than compatible dimensions, on A, B, and C.
This equation is fundamental in control theory and the function sylvester(A,B,C) has been part of MATLAB for many years. The standard, inhomogeneous, Sylvester equation involves three nonzero matrices, A, B, and C.