A Theoretical Understanding of Self-Correction through In-context Alignment