We compute the partition function of four-dimensional abelian gauge theory on a general fourtorus T 4 with flat metric using Dirac quantization. In addition to an SL(4, Z) symmetry, it possesses SL(2, Z) symmetry that is electromagnetic S-duality. We show explicitly how this SL(2, Z) 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 SL(2, Z) × SL(4, Z) symmetry. If we identify the couplings of the abelian gauge theory τ = θ 2π + i 4π e 2 with the complex modulus of the T 2 torus τ = β 2 + i 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 SL(2, Z) 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 SL(2, Z) acts suitably. For the 4d gauge theory, which has a Lagrangian, this product redistributes when using path integral quantization. *