Measurements from magnetometers and inertial sensors (accelerometers and gyroscopes) can be combined to give 3D orientation estimates. In order to obtain accurate orientation estimates it is imperative that the magnetometer and inertial sensor axes are aligned and that the magnetometer is properly calibrated for both sensor errors as well as presence of magnetic distortions. In this work we derive an easy-to-use calibration algorithm that can be used to calibrate a combination of a magnetometer and inertial sensors. The algorithm compensates for any static magnetic distortions created by the sensor plat- form, magnetometer sensor errors and determines the alignment between the magnetometer and the inertial sensor axes. The resulting calibration procedure does not require any additional hardware. We make use of probabilistic models and obtain the calibration algorithm as the solution to a maximum likelihood problem. The efficacy of the proposed algorithm is illustrated using experimental data collected from a sensor unit placed in a magnetically disturbed environment onboard a jet aircraft.