Until the gavel drops, it’s yours to sell. Keep in mind that to deliver clear title to the buyer you’ll have to pay off the mortgage and any other liens (usually from the proceeds of the sale), so you have to have at least a little bit of equity above the balance of the mortgage to pull this off.
Keep in mind that a potential buyer who is in a position to buy the property at the courthouse in the foreclosure sale may be able to get it for less than the price at which you can sell. The foreclosure extinguishes all junior liens, so a buyer at the courthouse steps wouldn’t be responsible for paying off, for example, a second mortgage if the first mortgage were being foreclosed. You, however, would be responsible for paying off the second mortgage in this scenario if you were to sell before the foreclosure auction.