I'm trying to replace my brake pads but when I try to push one piston in, the other one will come out and when I push it back in the previous one will do the same... how do I fix this so that both pistons remain retracted?
You need to push both pistons at the same time. There are tools for doing this, but many of us use a large flat bladed screwdriver.
You need a steady firm pressure for a few seconds to force the fuild in the lever back into the reservoir. Have a look at this Park Tool video - about 4:10