Geologists have never visited Mars. They think that they know how the planet formed by studying the information they have from pictures taken by spacecraft. They can also compare Mars to other planets close by. So this is what may have happened 4.6 billions of years ago.
Mars was formed as small objects in the solar system began to clump together. This took about 100,000 years. Mars grew, and the force of gravity pulled in more objects. The heat caused by the impact of these objects eventually created the different layers of the planet. Gases were released as the planet cooled, and that created the atmosphere. Then, Mars started to get hit by meteors. Some of them created huge craters. This also happened on Earth and the moon at about the same time. The inner part of Mars was very hot and that caused volcanoes to develop and erupt. When the planet finally cooled the atmosphere became thinner.