Unlike the electroweak sector of the standard model of particle physics, quantum chromodynamics (QCD) is surprisingly symmetric under time reversal. As there is no obvious reason for QCD being so symmetric, this phenomenon poses a theoretical problem, often referred to as the strong CP problem. The most attractive solution for this requires the existence of a new particle, the axion—a promising dark-matter candidate. Here we determine the axion mass using lattice QCD, assuming that these particles are the dominant component of dark matter. The key quantities of the calculation are the equation of state of the Universe and the temperature dependence of the topological susceptibility of QCD, a quantity that is notoriously difficult to calculate, especially in the most relevant high-temperature region (up to several gigaelectronvolts). But by splitting the vacuum into different sectors and re-defining the fermionic determinants, its controlled calculation becomes feasible. Thus, our twofold prediction helps most cosmological calculations to describe the evolution of the early Universe by using the equation of state, and may be decisive for guiding experiments looking for dark-matter axions. In the next couple of years, it should be possible to confirm or rule out post-inflation axions experimentally, depending on whether the axion mass is found to be as predicted here. Alternatively, in a pre-inflation scenario, our calculation determines the universal axionic angle that corresponds to the initial condition of our Universe.