Using directions like upwards in this context is meaningless because you haven't defined how the piston is oriented.
During the compression stroke of a typical 4-stroke gasoline engine, the piston takes energy from the crankshaft. The reason the whole scheme still works is that you get a lot more energy back after the cumbustion during the power stroke. Add up the total energy moved between the piston and the crankshaft over a whole cycle (two rotations of the crankshaft), and you will find the total being positive from piston to crankshaft.
The exhaust stroke takes relatively little energy since in theory the exhaust valve is open during this time and little pressure is required to expell the exhaust gasses.
You missed the remaining stroke, which is intake. That also takes a little energy since the pressure in the piston is negative in a traditional engine during that time. The piston is sucking the air or air-fuel mixture into the cylinder during that time.
In summary, the intake, compression, and exhaust strokes all take some energy from the rotation of the crankshaft, but this is more than made up by the energy imparted onto the crankshaft during the power stroke. This is also one reason these engines need to be started mechanically. It takes at least two energy-robbing strokes before you get the first positive return. This initial energy has to be supplied externally by rotating the crankshaft, such as is done by the starter motor in your car or by your arm when you pull the ripcord of your lawnmower.
At best, a piston is supplying energy only 1/4 of the time. This is why 4 cylinders is a common number for multi-cylinder engines. One piston is on its power stroke. Some of this energy is used to power the other three cylinders, with the rest being the output of the engine. More cylinders makes the overall operation smoother. More of a flywheel is needed with less cylinders, especially when you get to less than 4.