You’re right that gravity pulls matter in, but it doesn’t always result in a sphere. Think of a formation of a planet as a magnet core. The magnet core can start as any shape. Then, as dust, ice, and other matter collects onto the core, it begins to fill gaps on the core, but it also stick to areas of the early planet that it cannot pass through (essentially, walls of matter stops the formation of a solid sphere). So, the matter sticks there and builds up over time and you get wacky shapes of planets. It’s actually pretty cool if you think about it, how planets can be so different than our own.
They can also be in donut shapes too.