As stated by the Department of Physics at the University of Illinois at Urbana-Champaign, we actually don't know why gravity exists: "We still don't know 'why' matter and energy has gravity" ("Q & A: Why do Planets have Gravity"). However, some scientific work has been done to relate gravity to mass and to space and time.

Newton was the first to connect gravity to mass. Using Kepler's astronomical data and model of the solar system, Newton was able to see that the "strength of gravity depends on how much mass there is and how far away you are from this mass" ("Q & A").

Later, Einstein completed further work to explain gravitational effects. Einstein developed a model called the "general theory of relativity." This model postulates that there is a special relationship between space and time. As objects move through space, they also must "move through time," but objects moving through time do not necessarily also have to move through space; they can remain at rest ("What Causes Gravity?"). However, when mass exists in space-time, the space-time relationship becomes distorted so that mass moving through time is also compelled to move through, or to "accelerate" through space; we call this acceleration through space gravity ("What Causes Gravity?"). Einstein described the presence of mass as bending space and time, so that the freely moving mass "with no other forces [acting] on it will follow a path that bends along wit the local geometry of space and time" ("Q & A").

Based on Einstein's theory, gravity is no longer a force at all but rather obligatory motion due to space and time being bent by mass.