Good question! The main reason is that Iron is dense when compared with other elements that make up the Earth.
By total volume, Iron is the most abundant element within the Earth, and Oxygen is the second most abundant. However, they're not distributed equally throughout the planet. Most of the iron is at the center, and relatively little is at the crust.
The best explanation we have right now is that, early in the Earth's history, everything was distributed more ore less uniformly. This created instabilities since sometimes more dense material was found above less dense material. These instabilities corrected themselves through convection, when iron sunk down to the center of the planet, while less dense elements like silicon and carbon percolated up to the surface. Over time, the Earth developed its distinct layers - iron rich core and outer core, magnesium-iron rich mantle, and silicon-aluminum-oxygen rich crust.