Let v1 and v2 be the speeds of two masses m and M, respectively, when they are at a separation d.
As they approach each other, the kinetic energy increases and GPE decreases. Hence, for the system,
Loss in GPE=Gain in KE
⇒(GPE)i−(GPE)f−KEf−KEi
⇒0−(−GMmd)=(12mv21+12Mv22)−0
GMmd=12mv21+12Mv22
As there is no external force on this system, its total momentum remains conserved.
Pi=Pf
0=mv1−Mv2
Combining the two equations, we have
v1=√2GM2d(m+M)
and v2=√2GM2d(m+M).