Based on the generalized minimal residual (GMRES) principle, Hu and Reichel proposed a minimal residual algorithm for the Sylvester equation. The algorithm requires the solution of a structured least squares problem. They form the normal equations of the least squares problem and then solve it by a direct solver, so it is susceptible to instability. In this paper, by exploiting the special structure of the least squares problem and working on the problem directly, a numerically stable QR decomposition based algorithm is presented for the problem. The new algorithm is more stable than the normal equations algorithm of Hu and Reichel. Numerical experiments are reported to confirm the superior stability of the new algorithm.