I assume that the thrust of engines on the two sides must be kept exactly equal otherwise the plane would start yawing. What mechanism is used to prevent this from happening?
It isn't. The rudder is used.
Yes, if there is asymmetry in thrust, the plane will yaw. And there is asymmetry in thrust. The engine thrust decreases slightly with wear and the engines are often worn out differently. There are effects like P-factor that shift the thrust axis depending on speed and attitude. And of course an engine may fail.
So thrust asymmetry happens and needs to be compensated. And it is compensated by generating some sideways force with the rudder. Either by pilot pushing the pedal, or by applying trim.
As @mins mentions, there are also effects that can cause asymmetry of drag, e.g. icing, and there are situations where the aircraft intentionally needs to fly somewhat sideways, e.g. cross-wind take-off and landing, so rudder is needed for many other things too. And when it's there, it can compensate thrust asymmetry (often, though, the engine failure dictates the minimum size of the vertical stabilizer and rudder).
Note, that P-factor occurs in single-engine propeller aircraft too, so they also have some thrust asymmetry.
This depends on the airframe and where the engines are mounted. The father inboard the engines the less of an issue a slight variation in thrust will cause. But as mentioned the rudder is the general correction for this yaw.
The closest thing I can think of (and its not really for thrust balance although that is a byproduct of its function) is a Synchronizer/Synchronizer which can be found on some multi engine propeller planes. These devices are mainly to prevent harmonic beats from becoming irritating to the occupants to do so they keep the propellors spinning at near identical RPM's. If you engines are in tune and generating about the same power you should get near identical thrust from them.
The MD80 did have an engine sync system that was able to match RPM's of the N1 or N2 stages of the jets. It seems that this may have been mainly for noise but symmetrical thrust would also have been a byproduct more or less.
Here is an interesting patent on engine sync systems