It's a rotten thing to have to go through Fairness. But the point is, if your employer wants to get rid of either you or your job, they will achieve that and there is relatively little you can do other than not beat yourself up, learn from the experience, and move on as smoothly as possible.
It is an absolute bogger to see someone being given 'your' job or the one you knew you were qualified for, but this is the organisation you work for at its 'best'. Been therre hun. Concentrate your energies in getting over this and moving on, and good luck.