The Lagrange mesh method is a very simple procedure to accurately solve eigenvalue problems starting from a given nonrelativistic or semirelativistic two-body Hamiltonian with local or nonlocal potential. We show in this work that it can be applied to solve the inverse problem, namely, to find the equivalent local potential starting from a particular bound state wave function and the corresponding energy. In order to check the method, we apply it to several cases which are analytically solvable: the nonrelativistic harmonic oscillator and Coulomb potential, the nonlocal Yamaguchi potential and the semirelativistic harmonic oscillator. The potential is accurately computed in each case. In particular, our procedure deals efficiently with both nonrelativistic and semirelativistic kinematics.