Chondrules, which are roughly millimeter-sized silicate-rich spherules, dominate the most primitive meteorites, the chondrites. They formed as molten droplets and, judging from their abundances in chondrites, are the products of one of the most energetic processes that operated in the early inner solar system. The conditions and mechanism of chondrule formation remain poorly understood. Here we show that the abundance of the volatile element sodium remained relatively constant during chondrule formation. Prevention of the evaporation of sodium requires that chondrules formed in regions with much higher solid densities than predicted by known nebular concentration mechanisms. These regions would probably have been self-gravitating. Our model explains many other chemical characteristics of chondrules and also implies that chondrule and planetesimal formation were linked.