The effects of Mg(2+) ions, in comparison with Na(+) ions, on DNA structure and conformational dynamics were studied by molecular dynamics simulations. Mg(2+) ions, with a stable hydration shell consisting of six water molecules, interact with DNA mainly through hydrogen bond interactions, which are sensitive to the local environment. Mg(2+) ions were found at the phosphate backbone and selectively in the major groove of G·C bases. The sequence-dependent specificity is of electrostatic nature and not caused by steric constraints. The adjacent N7 and O6 atoms at the guanine base create a negative potential environment and act as hydrogen bond acceptor for hydrated Mg(2+) ions, while the positively charged H atoms on the N6 amino group of adenine base repel the hydrated Mg(2+). The binding of Mg(2+) makes the DNA duplex more rigid compared to the Na-DNA system, as demonstrated by reduced conformational entropy and restricted local bending motion. The sequence-specific interaction between Mg(2+) and DNA molecules provides a hint into a rich condensation behavior of DNA in the sequence context by Mg(2+) ions.