A digital twin-based optimization procedure is presented for an ultraprecision motion system with a flexible shaft connecting the motor to the (elastic) load, which is subject to both backlash and friction. The main contributions of the study are the design of the digital twin and its implementation, assuming a two-mass drive system. The procedure includes the virtual representation of mechanical and electrical components, non-linearities (backlash and friction), and the corresponding control system. A procedure for digital twin-based optimization is also presented, in which the maximum absolute position error is minimized while maintaining accuracy with no significant increase in the control effort. The optimal settings for the controller parameters and for the backlash peak amplitude, the backlash peak time, and the hysteresis amplitude are then determined, in order to guarantee an appropriate dynamic response in the presence of backlash and friction. The surface quality of certain manufactured components, such as hip and knee implants, depends on the smoothness and the accuracy of the real trajectory produced in the cutting process that is strongly influenced by the maximum position error. The simulations and experimental studies are presented using a real platform and two references for trajectory control, and a comparison of four digital twin-based optimization methods. The simulation study and the real-time experiments demonstrate the suitability of the digital twin-based optimization procedure and lay the foundations for the implementation of the proposed method at an industrial level.