Differential equations play a noticeable role in engineering, physics, economics, and other disciplines. In this paper, a general approach is suggested to solve a variety of linear and nonlinear ordinary differential equations (ODEs). With the aid of certain fundamental concepts of mathematics, Fourier series expansion and metaheuristic optimization methods, ODEs can be represented as an optimization problem. The aim is to minimize the weighted residual function (error function) of the ODEs. The boundary and initial values of ODEs are considered as constraints for the optimization model. Generational distance metric is used for evaluation and assessment of approximate solutions versus exact solutions. Two ODEs and one mechanical problem are approximately solved and compared with their exact solutions. The optimization task is carried out using different optimizers including the particle swarm optimization and the water cycle algorithm. The optimization results obtained show that the metaheuristic algorithms can be successfully applied for approximate solving of different types of ODEs.