The following papers prove this:

MR0682456 Reviewed Joris, Henri Une C∞-application non-immersive qui possède la propriété universelle des immersions. (French) [A nonimmersive C∞ mapping having the universal property of immersions] Arch. Math. (Basel) 39 (1982), no. 3, 269–277.

MR0833407 Reviewed Duncan, John; Krantz, Steven G.; Parks, Harold R. Nonlinear conditions for differentiability of functions. J. Analyse Math. 45 (1985), 46–68. (Reviewer: Wiesław Pleśniak) 26E10 (58C25)

MR2179865 Reviewed Myers, Robert An elementary proof of Joris's theorem. Amer. Math. Monthly 112 (2005), no. 9, 829–831. (Reviewer: Clifford E. Weil) 26A24