Composition of terrestrial planets records planetary accretion, core-mantle and crust-mantle differentiation, and surface processes. Here we compare the compositional models of Earth and Mars to reveal their characteristics and formation processes. Earth and Mars are equally enriched in refractory elements (1.9 $times$ CI), although Earth is more volatile-depleted and less oxidized than Mars. Their chemical compositions were established by nebular fractionation, with negligible contributions from post-accretionary losses of moderately volatile elements. The degree of planetary volatile element depletion might correlate with the abundances of chondrules in the accreted materials, planetary size, and their accretion timescale, which provides insights into composition and origin of Mercury, Venus, the Moon-forming giant impactor, and the proto-Earth. During its formation before and after the nebular disks lifetime, the Earth likely accreted more chondrules and less matrix-like materials than Mars and chondritic asteroids, establishing its marked volatile depletion. A giant impact of an oxidized, differentiated Mars-like (i.e., composition and mass) body into a volatile-depleted, reduced proto-Earth produced a Moon-forming debris ring with mostly a proto-Earths mantle composition. Chalcophile and some siderophile elements in the silicate Earth added by the Mars-like impactor were extracted into the core by a sulfide melt. In contrast, the composition of Mars indicates its rapid accretion of lesser amounts of chondrules under nearly uniform oxidizing conditions. Mars rapid cooling and early loss of its dynamo likely led to the absence of plate tectonics and surface water, and the present-day low surface heat flux. These similarities and differences between the Earth and Mars made the former habitable and the other inhospitable to uninhabitable.