Exactly this is capitalism at it's finest. Working on open source projects by itself has rewards :
1) you probably use it, so you use it yourself and your own situation gets better.
2) you get the efforts of others in addition to your own because of the open source nature (e.g. your code gets maintained, fixed, reviewed, ...)
3) There are plenty of companies (Google, FB, Red Hat, ...) that will give you a great job because of open source contributions you have made (and more general, reputational benefits)
FOSS projects have to start out private for someone to be able to make the decision to open source them in the first place. If everything belonged to the state, you might end up having a pretty hard time convincing the state to allow other states to fork/use it.
It's an economic activity with first order benefits and positive externalities.