Dark energy, which is the reason for the accelerated expansion, is counteracted by gravitation. The closer two objects are together, the weaker dark energy gets in comparison to gravitation between the objects (there is less space between them, thus less dark energy, and gravitation is stronger than at greater distance). Now, I am not an astrophysicist, but I can imagine that Milky Way and Andromeda were so close to each other, that the gravitational attraction between them was stronger than the dark energy trying to push them apart.