If the B-mode signal in the cosmic microwave background polarization seen by the BICEP2 experiment is confirmed, it has dramatic implications for models of inflation. The result is also in tension with Planck limits on standard inflationary models. It is, therefore, important to investigate whether this signal can arise from alternative sources. If so, this could lessen the pressure on inflationary models and the tension with Planck data. We investigate whether vector and tensor modes from primordial magnetic fields can explain the signal. We find that, in principle, magnetic fields generated during inflation can indeed produce the required B mode, for a suitable range of energy scales of inflation. In this case, the primordial gravitational wave amplitude is negligible, so that there is no tension with Planck and no problems posed for current inflationary models. However, the simplest magnetic model is in tension with Planck limits on non-Gaussianity in the trispectrum. It may be possible to fine tune the magnetogenesis model so that this non-Gaussianity is suppressed. Alternatively, a weaker magnetic field can pass the non-Gaussianity constraints and allow the primordial tensor mode to be reduced to r≃0.09, thus removing the tension with Planck data and alleviating the problems with simple inflationary models.