It is completely legal. When your home goes into foreclosure and up for auction, you are only responsible for the remaining balance of your loan. If the auction price does not cover the balance, you are required to settle the remaining balance. However, in your situation, the price exceeded the amount due, leaving you a surplus. Contact your county. You will receive a 10-99, and will have to claim it on your taxes.
Hope this helps!