David Hilbert (born today in 1862) was one of the greatest mathematicians of the 20th century. He made incisive contributions to a remarkable range of mathematical fields, published a best selling textbook on mathematical methods in physics, laid out a famous list of twenty-three unsolved problems which still challenge the field's practitioners and was a kind of philosophical godfather to at least two generations of mathematicians. Under his influence German mathematics reached its zenith before it was scattered apart by the rise of totalitarianism.
Perhaps less known is Hilbert's friendly and sometimes not-so-friendly rivalry with Einstein. Einstein's two most serious mathematical competitors were Hilbert and Henri Poincare. Poincare came close to discovering special relativity. Hilbert came close to discovering the equations of general relativity. Unlike Einstein, both were men of prodigious mathematical talent. Einstein's own mathematical shortcomings are well known; he had to learn most of the mathematics he needed for cracking open general relativity from friends and colleagues, most notably Marcel Grossmann.
In 1915 Hilbert came very close to publishing the equations of general relativity before Einstein did. Einstein had given a lecture in Berlin on his tentative attempts at formulating the equations. Hilbert was in the audience and doubled his efforts to find the right formulas. In the end Einstein ended up finding the correct form of the equations just a few days before Hilbert. It's quite likely that Hilbert would have gotten there first had Einstein gotten stalled for some reason.
However this story may paint a false picture of why it was Einstein and not Hilbert who ended up inventing general relativity. The short answer is that it was not mathematics but physics that was at the heart of relativity theory. Hilbert was the greater mathematician among the two, but Einstein's physical insights and visualization of thought experiments were unparalleled in the history of physics. Like a select few scientists - Feynman, Faraday and Fermi come to mind - he saw the physics first and then dragged the math behind him later. He had already viewed gravity as a field of spacetime which could be deformed and pulled by the time he started thinking about the equations. Hilbert, like more traditional physicists, tried to solve the equations first and get to the physical picture later.
Generally speaking, truly great and successful physicists are ones who see the physical picture first, and Einstein was in the front rank of this group. Thus even if Hilbert had gotten to the equations first, Einstein would still have won the day. His 1915 paper on relativity contains not only the equations but all kinds of amazing physical insights that would lead to observations on the bending of spacetime and the expansion of the universe.
The last word belongs to Einstein, not because he was a great scientist but because he was a great human being. After the whole nail-biting race was over, he wrote a sensitive letter to Hilbert that exuded generosity, acknowledged his own flaws and sought reconciliation and friendship.
The letter is a role model for competition and disagreement, and should be a must read in this era of political disagreement and conflict. Ultimately Einstein showed that while physics is important, being good human beings is even more important.