Magnetic fields are present on all scales in the Universe. While we understand the processes which amplify the fields fairly well, we do not have a ``natural'' mechanism to generate the small initial seed fields. By using fully relativistic cosmological perturbation theory and going beyond the usual confines of linear theory we show analytically how magnetic fields are generated. This is the first analytical calculation of the magnetic field at second order, using gauge-invariant cosmological perturbation theory, and including all the source terms. To this end, we have rederived the full set of governing equations independently. Our results suggest that magnetic fields of the order of 10-30- 10-27 G can be generated (although this depends on the small scale cut-off of the integral), which is largely in agreement with previous results that relied upon numerical calculations. These fields are likely too small to act as the primordial seed fields for dynamo mechanisms.