Most people underlined the role played by the expansion of the universe, however, this expansion is not actually 'pushing' Andromeda away, it is only changing the frequency of the light emitted by Andromeda, so that we can associate a speed that would give the same Doppler effect (when an object is emitting a wave, like a fire truck, and going away, the received wave is at a lower frequency than the emitted one).
The more correct answer would be that the two galaxies are the result of a long accretion (of gas) and merging history (with another now disappeared smaller galaxy). Each of this event changed the speed of Andromeda and the Milky Way relative to a rest frame, so that the speed nowadays is the result of these numerous interactions, the gravitational pull of one on each other as well as the gravitational potential created by the local group (the group of galaxies in which belongs the Milky Way and Andromeda, containing for example the Great Magellanic Cloud).
If you want an illustration of what the formation of a galaxy look like, you can take a look at a simulation, like the Horizon-AGN simulation. There is a video (here: http://www.horizon-simulation.org/movies/horizon-AGN_denseproj.avi) in which you can see galaxies forming within a cosmological framework. In the movie, each bright dot is a forming galaxy. 562
You can see many very complicated interactions, so that two galaxies close to each other today may have formed very far away and were brought close by.
In the case of our two galaxies, because we are located in a group of galaxies you can expect that the initial locations of our two galaxies were actually very different compared to nowadays. The reason for that is that in the past, as very correctly explained by userLTK, distances were smaller so that the gravitational pull of objects was (in average on the local group) stronger than today. Therefore, the interaction of the matter that is now forming the group of galaxy in which lay the MW and Andromeda had gravitational interactions somehow stronger so that the relative speed was higher. Because of that, our two galaxies have been traveling for 14 Gyr from some remote location to their current location, where they happen to be going one toward each other. 562