How did Mars form an atmosphere in the first place if it has no magnetic field to keep it from getting stripped away?
One of my favorite moments in my formal education came when I took a second year geology elective called "Geologic Time". We spent some time discussing the formation of the solar system. If I may, I'll give a brief summary, as it will give some context to your question.
Many billions of years ago, a large star composed of hydrogen formed. Due to the high pressure and temperature in that star, new elements formed by fusion, with the largest element formed being iron. The star had a relatively short life, collapsed, and exploded in a supernova. During the explosion, neutrons, amongst other things were sprayed around the already existing matter. Those neutrons, being neutral, tended to "stick" to other nuclei. This, combined with beta decay explains the formation of elements larger than iron.
A wisp of the dust from that supernova began to coalesce into a spinning disk, due to gravity and angular momentum. The larger amount of material in the center of the disk was pulled together by gravity strongly enough to create fusion. Thus our sun was born. Within the spinning disk, some material was naturally volatile (e.g. water, methane, etc.). Some of the material tended towards becoming solid. One such material was silicon. The silicon reacted with oxygen to form silicon dioxide (I think). The silicon dioxide tended to form solid spheres in much the same way that hail is formed within a thunderstorm. These little spheres of silicon were commonly the size of ball bearings or actual hailstones. We call them chondrules.
When the sun ignited, it created an outwards stream of particles, which we call the solar wind. The particles in the solar wind easily pushed volatile molecules like water and methane outwards, away from the Sun. However, the solar wind was not able to push silicon chondrules outwards very much, due to their large size (compared to gas molecules). Thus, the inner planets are made of rocky silicon, while the outer planets, beginning with Jupiter, are made of volatile gaseous compounds.
The inner rocky planets slowly expanded in size due to falling rocks (and later comets). Eventually, radioactive decay in the Earth (and Mars) increased the inner temperatures of these planets enough that they melted inside, that is, they changed from largely hetorogenous chunks of rock to something more like today's planets. As the inside of the Earth melted, the most dense elements sank to the center. The most common dense element was iron. Thus the Earth got its iron core. Due to the motion of that solid/liquid iron core, the Earth developed a natural magnetic field. That magnetic field deflected the high speed charged particles in the solar wind around the Earth, thus protecting our atmosphere from being blown away. For some reason that I am not aware of, Mars did not develop a significant magnetic field. Thus, over time, Mars lost its atmosphere due to molecular collisions with particles in the solar wind.
I think the best way to answer your question would be to say that Mars got its atmosphere the same way that Earth did. Likely from some combination of comet collisions bringing volatiles from the outer solar system, and from volcanism releasing volatiles that were initially trapped in the rocky Earth. The solar wind acted on Mars' atmosphere over many billions of years, slowly removing it molecule by molecule. It wouldn't have happened right away...it would have taken a very long time to thin Mar's atmosphere significantly.
There...a bit longer than what I intended, but not bad considering what I described.