Analyzing the Inner Workings of Transformers in Compositional Generalization