There are various versions of the Maxwell's demon scenario, but generally they are deliberately posed in a way not to violate the first law. As presented in much of the popular literature, the demon (explicitly or implicitly) allows the same number of particles to go in each direction. That means the internal energy on one side increases while on the other side it decreases. That means the demon generates a pressure difference between the two chambers, which can then be used to drive a piston and do external work as you describe. The work comes from the original internal energy of the system, though, so the system loses energy to the surround as the work is done. To restore the system to its original state, you would need to dissipate that external work back into heating the system. In this case the apparent violation of the 2nd law is that the demon generates a pressure-separated state, which is lower-entropy than the initial state, without (apparently) doing any work.
In a version of the demon that's a little closer to the original, the demon moves more low-energy particles than high-energy, so as to keep the internal energy the same on both sides. The high-energy side ends up with fewer particles and a higher temperature, the lower side has more particles and a lower temperature, but the pressures (proportional to the internal energy) are equal. That's more subtle, because now you can't extract work just by driving a piston with a pressure difference. You can, however, drive a heat engine, which can produce work as it transmits heat from the high-T side to the low-T side. However, the work still comes from the internal energy of the system, so there is no violation of the first law. In fact, now the violation of the 2nd law is not so obvious - since there is equal energy going into and out of each side, it would seem that any increase in entropy due to heat flow into a chamber would be compensated by a decrease due to heat flow out. It turns out, though, that the net particle flux is associated with an increase in entropy of the low-T side and a decrease on the high-T side, and that the decrease outweighs the increase, so the sorting is in fact connected with lowering the entropy of the system.