This is an anecdote, probably invented to complement the other anecdote about Laplace. It is hard to say why Lagrange in particular was picked to fill the part, as he is a bad fit. Probably, it is just that he was seen with Napoleon at Mass, and the name is suitably famous. Many Lagrange's works are translated into English, e.g. his opus magnum Analytical Mechanics, so one can do a search to see if he mentions God "endlessly". In fact, he does not mention him there at all.
Instead, we find God mentioned in the translators' biographical remarks on Lagrange's religious views, which do not exactly leave an impression of excessive devoutness. To the contrary, he was so reserved in his expressions of faith that some considered him an atheist, although this is probably an exaggeration. And he definitely did not think that God can explain much in mechanics, as the anecdote suggests (with Lagrange allegedly exclaiming:"But it is a beautiful hypothesis that explains so many things"):
"It appears that he had no interest in Maupertuis’
attempt to harmonize the Principle of Least Action with the existence of a deity. In
his view, it was simply a mathematical proposition... The endless discussions over this question, which appeared to lead
nowhere, appeared meaningless to Lagrange. Hence, he undertook to describe the
mechanical behavior of material systems without any metaphysical commentary
at all... Euler had written a work — Lettres a
une Princesse d’Allemagne — in which he attempted to reconcile science and
religion. Lagrange held that this effort was inappropriate for a man of science.
[...] The question of what were Lagrange’s religious beliefs is difficult to answer.
Authors who have investigated this aspect of Lagrange’s personality have not
been able to reach a conclusion... Many of Lagrange’s colleagues were not members of the
Christian church and therefore, Lagrange, as prudent as he was in the conduct of
his personal affairs, kept his religious beliefs to himself. He may have viewed
religious belief as he did metaphysical arguments, that is, with the attitude that they
would not produce any tangible results.
Secondly, his private library contained
a number of books on religion — not all on the Christian religion but bibles and
works on various world religions. These books indicate an interest in religion on
the part of Lagrange... On the other hand, in his letters to his family, where it is not necessary to be
prudent, there are no references to religion. Even when a death in the near family
seemed to require words of consolation such as the death of his mother, brother
and father, there are none.
But there was never any indication from Lagrange that he was an atheist. In fact, he attended Mass regularly and during his sojourn in Paris during
the Empire, there is a record of his many chance meetings with Napoleon at Mass. Lagrange’s name is found in the Dictionnaire des Athees Anciens et Modernes by
Sylvain Marechal along with the names of Napoleon, Frederick the Great, Laplace,
Monge, de Prony, Lalande, Peyrard, Fourcroy, etc. Marechal noted that these men
held that it was impossible to prove the existence of God. He referred to them
as atheists but it would have been more accurate for Marechal to point out that a
declaration of agnosticism rather than atheism was really in question."