Diamond and the graphite in a pencil are both pure carbon.
The only difference is how the carbon atoms are arranged. In diamond every atom is bonded rigidly to four neighbours in a three-dimensional lattice; in graphite the atoms form flat sheets that slide over each other, which is why pencils write.
Why is this true?
At Earth's surface, diamond is actually the less stable form and is very slowly turning into graphite, far too slowly to notice. Diamonds form under immense pressure deep in the mantle and are carried up by volcanic eruptions.
More facts in this category
More about science & nature
Science and nature supply facts that feel like they break the rules until you see the mechanism. Hot water freezing before cold, glass that flows, lightning hotter than the surface of the Sun. Each entry keeps the physics or chemistry short and readable, states plainly where the effect is still debated, and links to a source for anyone who wants the full derivation rather than the headline.