There are several approaches to this kind of question. For the diagram you show, with only a few molecules with known positions and velocities, you’re working in the realm of Newtonian mechanics, not statistical mechanics or thermodynamics. In that realm, everything is determined and known, entropy is therefore always zero, and it’s not meaningful to talk about the 2nd law of thermodynamics: it’s true by definition. With a slightly more disordered/complex system, you enter statistical mechanics. Here, you talk about probabilities of things happening, including the probability of changes in entropy. In that limit entropy can go down, it’s just unlikely. As the system gets bigger, it gets more unlikely. As you eventually get to the large system limit, the probability of deviations is effectively zero, and the laws of thermodynamics are rigidly applicable. But there’s a more basic point to make: Expansion of a piston turns heat into work, and there’s no violation of the 2nd law in that case. That’s just a standard expansion process.