I tried to research this and I'm not sure if there is a consensus where the $CO_2$, $N_2$, etc. comes from on terrestrial planets like Venus, Earth and Mars. Possible sources would be accretion from the protoplanetary disk, outgassing (which I would think would require plate tectonics/ molten core) or reactions at the surface caused by stellar radiation. Also, reactions in the atmosphere can alter the composition. Would it be a combination of all of the above and, if so, do we know if one of the processes dominated over the others? Does anybody have a good link that discusses this?
Three decent answers in comments converted to a community wiki.
A quite recent study about the origin and development of terrestrial planet atmospheres is the 2015 paper "The Atmospheres of the Terrestrial Planets: Clues to the Origins and Early Evolution of Venus, Earth, and Mars" (Baines et al.) includes a fairly comprehensive analysis of chemical evidence from Earth and from Martian meteorites and observations of the atmosphere of Venus.
The general consensus is that much of the atmospheric gases were brought through comet and asteroid impacts.
The pre-planetary disk would have been centrifugally sorted. Many iron, stone, carbonacious meteorites, watery, ice-iron-rock agglomerates would have brought a very diverse amalgam of different stones, ices, and room temperature gas elements, throughout the formation. CO2 and N2 objects are a bit less stable in direct solar radiation at 1 AU. CO2 and N2 would have pounded into the world in considerable quantities throughout the accretion process, with a wide range of particulate sizes from microns to kilometers in diameter without a marked difference in element type from start to end.