Massive stars play a crucial role in the production of heavy elements and in the evolution of the interstellar medium, yet how they form is still a matter of debate. We report high-angular-resolution submillimeter observations toward the massive hot molecular core (HMC) in the high-mass star-forming region G31.41+0.31. We find that the evolution of the gravitational collapse of the HMC is controlled by the magnetic field. The HMC is simultaneously contracting and rotating, and the magnetic field lines threading the HMC are deformed along its major axis, acquiring an hourglass shape. The magnetic energy dominates over the centrifugal and turbulence energies, and there is evidence of magnetic braking in the contracting core.