We compute the partition function of four-dimensional abelian gauge theory on a general four-torus T 4 with flat metric using Dirac quantization. In addition to an $$ \mathrm{S}\mathrm{L}\left(4,\;\mathcal{Z}\right) $$ symmetry, it possesses $$ \mathrm{S}\mathrm{L}\left(2,\;\mathcal{Z}\right) $$ symmetry that is electromagnetic S-duality. We show explicitly how this $$ \mathrm{S}\mathrm{L}\left(2,\;\mathcal{Z}\right) $$ S-duality of the 4d abelian gauge theory has its origin in symmetries of the 6d (2, 0) tensor theory, by computing the partition function of a single fivebrane compactified on T 2 times T 4, which has $$ \mathrm{S}\mathrm{L}\left(2,\;\mathcal{Z}\right)\times \mathrm{S}\mathrm{L}\left(4,\;\mathcal{Z}\right) $$ symmetry. If we identify the couplings of the abelian gauge theory $$ \tau =\frac{\theta }{2\pi }+i\frac{4\pi }{e^2} $$ with the complex modulus of the T 2 torus $$ \tau ={\beta}^2+i\frac{R_1}{R_2} $$ , then in the small T 2 limit, the partition function of the fivebrane tensor field can be factorized, and contains the partition function of the 4d gauge theory. In this way the $$ \mathrm{S}\mathrm{L}\left(2,\;\mathcal{Z}\right) $$ symmetry of the 6d tensor partition function is identified with the S-duality symmetry of the 4d gauge partition function. Each partition function is the product of zero mode and oscillator contributions, where the $$ \mathrm{S}\mathrm{L}\left(2,\;\mathcal{Z}\right) $$ acts suitably. For the 4d gauge theory, which has a Lagrangian, this product redistributes when using path integral quantization.