In a Carnot engine, represented above, it is said that work is done by the system, but in reality , work is done by the system only in the isothermal and adiabatic expansions (from 1 to 3 in the picture above) then work is done on the system, as it contracts in an isothermal and adiabatic process (3 to 1). So why do we say that work is done only by the system in this engine, as the above pictures suggests, does it refer only to the first 2 processes (the expansions)? And the same goes to the reverse process, the refrigerator engine, where it is said that work is done on the system, and obviously in that case i still don't understand why it is said that work is done on the system.
I think you need to look at the “system” as the working fluid (e.g. an ideal gas) undergoing the Carnot cycle, and that the work done by the system is the net work for the entire cycle (the area enclosed by the PV diagram for the cycle). That work is positive, meaning net work is done by the system over the entire cycle, even though for two of the processes (isothermal and adiabatic compression) work is done on the system in order to complete the cycle.
Hope this helps.