What are pacemakers and Implantable cardioverter defibrillators (ICDs)?
Cardiac pacemakers and ICDs are small, battery-operated devices implanted under the skin near the heart. A pacemaker maintains an adequate heart rate through low-voltage electrical impulses. An ICD monitors the heart rate and then treats irregular heartbeats (arrhythmias) by delivering electric shocks. It also has a pacemaker built into it and can pace the heart and restore normal heart rhythm.
To treat:
A slow heartbeat
Irregular heartbeats
Overcome an electrical blockage

What are the potential risks in scanning a patient with these devices?

Heating induced by the RF field around the pacing lead tip has the potential to damage the myocardial tissue.
Electrical currents induced on the conductive surface of the case may cause thermal tissue damage and discomfort.

Movement or torque
Pacemaker or lead movement may arise from ferromagnetic components being affected by the magnetic field.

Modification of function
Vibrations may cause temporary or permanent malfunctioning/damage of the device. 
Deliver inappropriate shocks.

Trigger of rapid pacing
Gradient and RF fields have the potential to induce life-threatening ventricular tachyarrhythmia.

MRI Safe?

A patient presenting at an MRI Centre with a pacemaker  or ICD should not be scanned.

References : pacemaker safety : pacemaker safety : pacemaker safety : pacemaker safety