Interactions between divalent cations (Mg2+and Ca2+) and highly charged single stranded DNA (ssDNA) and double stranded DNA (dsDNA), as well as stacking interactions, are important in a variety of problems, including nucleosome stability and phase separation in nucleic acids. Quantitative techniques accounting for ion-DNA interactions are needed to obtain insights into these and related problems. Towards this end, we created a computational model that explicitly takes into account monovalent and divalent ions, within the framework of the sequence-dependent coarse-grained Three Interaction Site (TIS) model for DNA. Molecular simulations of the rigid 24 base-pair (bp) dsDNA and flexible ssDNA sequences, dT30and dA30, in a buffer containing Na+and Cl−, with varying amounts of the divalent cations, are used to show that the calculated excess number of ions around the dsDNA and ssDNAagree quantitatively with ion-counting experiments. Using an ensemble of all-atom structures generated from coarse-grained simulations, we calculated the Small Angle X-ray Scattering (SAXS) profiles, which are also in excellent agreement with experiments. Strikingly, recapitulation of all the experimental findings was achieved without adjusting any of the parameters in the energy function to fit the data. At a molecular level, we find that Mg2+and Ca2+sense the differences between the major and minor grooves in dsDNA even though they are masked in ion-counting and SAXS experiments. The smaller Mg2+binds predominantly to the minor grooves and phosphate groups whereas Ca2+binds specifically only to the minor groove. The dA30conformations are dominated by stacking interactions, resulting in structures with considerable helical order. In contrast, the near cancellation of the favorable stacking and unfavorable electrostatic interactions leads to dT30populating an ensemble of heterogeneous conformations.