We present the first two-loop calculation of the heavy quark energy shift in lattice nonrelativistic QCD (NRQCD). This calculation allow us to extract a preliminary prediction of $m_b(m_b, n_f = 5) = 4.25(12)$ GeV for the mass of the b quark from lattice NRQCD simulations performed with a lattice of spacing $a=0.12$fm. Our result is an improvement on a previous determination of the b quark mass from unquenched lattice NRQCD simulations, which was limited by the use of one-loop expressions for the energy shift. Our value is in good agreement with recent results of $m_b(m_b) = 4.163(16)$ GeV from QCD sum rules and $m_b(m_b, n_f = 5) = 4.170(25)$ GeV from realistic lattice simulations using highly-improved staggered quarks. We employ a mixed strategy to simplify our calculation. Ghost, gluon and counterterm contributions to the energy shift and mass renormalisation are extracted from quenched high-beta simulations whilst fermionic contributions are calculated using automated lattice perturbation theory. Our results demonstrate the effectiveness of such a strategy.