In this paper, a systematic model-based approach for the state estimation of permanent magnet synchronous motors to build up sensorless drives is presented. A moving horizon estimation algorithm, an optimization-based scheme that yields excellent performance, is applied for the speed and position estimation. Under mild assumptions, an optimal problem of equality constrained quadratic programing type has been solved each iteration. The steady state and transient performance of the proposal are evaluated considering different horizon lengths. The influence of the latter is analyzed in terms of estimation error and computational burden. Besides, the behavior of the scheme is analyzed with different current control loops, i.e., with conventional proportional integral regulators and with model predictive control, highlighting its adaptability and good response in control schemes with completely different features. The performance is compared with that of an extended Kalman filter and the results prove the effectiveness of the proposed control scheme in terms of the estimation accuracy and robustness even at low speed operation. The algorithm has been efficiently implemented showing the real-time feasibility of the proposed approach up to 10-kHz sampling rate.