How were four wires replaced with two wires in early telephones?
How was this possible? Wouldn't the current from microphone affect
speaker on the same side?

The modern telephone is wired in a Wheatstone bridge arrangement like this: -

So, if you ensure that the telephone network line impedance (\$Z_{LINE}\$) is controlled then, theoretically, any signal produced by the microphone is dramatically reduced into the local earpiece. It won't be a perfect cancellation but it'll be pretty good.
Amended picture originally from here. It might be easier to understand this diagram: -

Picture from here.
And, all throughout the network there are line amplifiers that need to translate from 2 wire to 4 wires so that amplifier circuits can be added: -

Picture from here. Then another hybrid transformer is used to reconvert the 2-way (4-wire) amplified signals back 2-wire: -

Here's an example of an early telephone anti-sidetone circuit using the same principle as the hybrid transformer: -

Picture from this website.