While there are different reasons why a bank chooses to sell a mortgage, the most common is that the borrower is delinquent on their payments (aka a non-performing note).
Banks typically place these liabilities into a bundle or package, and sell them at a deep discount.
In other instances, banks may also choose to sell performing notes. This usually happens after the bank has collected interest payments for many years and they now want to use their capital for other purposes. These performing notes are often sold to other banks, mortgage lenders or investment firms at a moderate discount.