Role of calcium chloride during competent cell preparation
Although we do not have any conclusive proof about how $CaCl_2$ works, the suggested mechanism is much similar to what you have suggested. When $CaCl_2$ dissociates in aqeous solution, $Ca^{2+}$ ions bind to the phosphate groups in DNA as well as the inner core of lipopolysaccharides in cell membrane (Dagert et al, 1979), neutralizing them. After this, heat shock allows uptake of this DNA into the host (how heat shock works is also not known; it is suggested that increased temperature creates pores in cell membrane, see this answer). For visual representation, see this diagram (from here):



Regarding $MgCl_2$, the role of $Mg^{2+}$ is not completely understood either. However, $Mg^{2+}$ is known to promote cell growth (Hanahan et al, 1983). It might be because $Mg^{2+}$ is an important factor required for functioning of ribosomes (Nierhaus et al, 2014). Anyhow, its role in increasing competence seems to be lesser than that of $Ca^{2+}$.

P.S.: during the experiment, $Mn^{2+}$, $Rb^+$ and $K^+$ also work effectively along with $Ca^{2+}$ (Hanahan et al, 1983).

References:


Dagert, M.; Ehrlich, S. (1979). "Prolonged incubation in calcium chloride improves the competence of Escherichia coli cells". Gene. 6 (1): 23–28. doi:10.1016/0378-1119(79)90082-9. PMID 383576
Douglas Hanahan, Studies on transformation of Escherichia coli with plasmids, Journal of Molecular Biology, Volume 166, Issue 4, 1983, Pages 557-580, ISSN 0022-2836, http://dx.doi.org/10.1016/S0022-2836(83)80284-8.
Nierhaus K.H. $Mg^{2+}$, $K^+$, and the ribosome J. Bacteriol.  2014 196 3817-3819