In this paper, the finite difference nonlinear Poisson-Boltzmann (NLPB) equation is used to calculate the electrostatic contribution to the B to Z transition of DNA using detailed molecular structures of each DNA forth. The electrostatic transition free energy is described as a balance between the change in intramolecular Coulombic interactions and charge-dependent interactions between the DNA and the solvent. As in many prior studies, we find that the larger electrostatic repulsions among the more closely spaced Z-DNA phosphates destabilize this form compared to B-DNA in the absence of solvent. However, as a result of the more compact three dimensional geometry of Z-DNA, both water and salt are found to strongly stabilize this conformation to the extent that the total electrostatic free energy favors the B to Z transition in aqueous solution. Water acts not only by screening the inter-phosphate repulsions but also by solvating both charged and polar groups on Z-DNA more favorably than B-DNA. In addition, Z-DNA is stabilized by a substantially higher concentration of nearby counterions than B-DNA. The relative stabilization of Z-DNA by salt increases with increasing bulk salt concentration, leading to the high-salt B to Z transition. We find that the salt dependence of the B to Z transition free energy calculated with the NLPB equation agrees reasonably well with experimental results. Since electrostatic interactions are found to favor the Z-form, nonelectrostatic forces must be responsible for the relative stability of B-DNA in solution. An analysis of these forces suggests that the conformational entropy may play an important role.