The origin of nitrogen fixation is an important issue in evolutionary biology. While nitrogen is required by all living organisms, only a small fraction of bacteria and archaea can fix nitrogen. The prevailing view is that nitrogen fixation first evolved in archaea and was later transferred to bacteria. However, nitrogen-fixing bacteria are far larger in number and far more diverse in ecological niches than nitrogen-fixing archaea. We therefore propose the bacteria-first hypothesis, which postulates that nitrogen fixation first evolved in bacteria and was later transferred to archaea. As >30,000 prokaryotic genomes have been sequenced, we conduct an in-depth comparison of the two hypotheses. We first identify the six genes involved in nitrogen fixation in all sequenced prokaryotic genomes and then reconstruct phylogenetic trees using the six nitrogen-fixing (Nif) proteins individually or in combination. In each of these trees the earliest lineages are bacterial Nif protein sequences and in the oldest clade (group) the archaeal sequences are all nested inside bacterial sequences, suggesting that the Nif proteins first evolved in bacteria. The bacteria-first hypothesis is further supported by the observation that the majority of nitrogen-fixing archaea carry the major bacterial Mo (molybdenum) transporter (ModABC) rather than the archaeal Mo transporter (WtpABC). Moreover, in our phylogeny of all available ModA and WtpA protein sequences the earliest lineages are bacterial sequences while archaeal sequences are nested inside bacterial sequences. Furthermore, the bacteria-first hypothesis is supported by available isotopic data. In conclusion, our study strongly supports the bacteria-first hypothesis.