From UEFA:
Swapping shirts at the end of a match is a symbol of respect between
opponents.
In fact, UEFA started a campign called Swap your Jersey – show Respect!
The first known case of swapping shirts was on 1931:
The gentlemanly ritual is believed to have begun in 1931, when France
beat England for the first time. The French players were so ecstatic
they asked the English players if they could have their jerseys as
keepsakes. The English obliged.
It was not until 1954 that exchanges took place at a World Cup
As swapping jerseys is consider a symbol of respect, This action isn't mandatory.
In fact, at most matched jerseys isn't been replaced at the end of the match.
There were several special cases that this switch hasn't been made,
The most famous case in history of refusing jersey exchange was on England - Argentina, 1966 World Cup quarter-final.
In 1966, England defeated Argentina, 1-0, in the quarterfinals, but
the play was so vicious that when England’s manager, Alf Ramsey, who
had referred to Argentina’s players as animals, spotted one of his
players exchanging jerseys with an Argentine, he stepped in and yanked
the jersey away.
Source
As for what is been done with those swapped jersey is for the players to decide,
they can do what there want with the jersey.
Many players treat it like trophies and other prizes they had won in their career.