An implantable medical device (IMD) is described that automatically detects the presence of an external magnetic field, such as that generated by an MRI device. The IMD includes a torque sensor configured to generate an output signal that varies as a function of a torque imposed on the torque sensor by an external magnetic field and a control module configured to control operation of the implantable medical device based on the signal output of the torque sensor.