In most instances, the IRS is a junior lienholder, to the primary (the 1st and 2nd mortgagor). Thereby, the IRS' lien is usually the one most recently recorded, and therefore, the most susceptible to being wiped off by a foreclosure of any prior recorded senior lien (1st or 2nd, etc.)
If IRS' lien is wiped off by a trustee's sale, the IRS still retains the right to redeem the property from the new owner within the following 120 days. If redeemed, the IRS would sell the property at its "redemption sale", presumably for a lot more than what it went for at the earlier trustee's sale. The sale proceeds go pro-tanto towards its incidental expenses, reimbursement to the redemption fund, the tax liability and then to the "party entitled" (the next junior lienor in priority at the trustee's sale). If IRS does not exercise its redemption right within the 120 days it will automatically expire.
I may be further reached by email (firstname.lastname@example.org) or through my website (www.bocadelrayattorneyatlaw.com). Please note, however, that the hiring of a lawyer is an important decision and should not be based solely upon advertisements or the like. Before you decide which attorney to retain, please ask for free written information about their (or my own) qualifications and experience. Also, providing an answer to your question does not constitute in any manner whatsoever a client-attorney relationship or privilege between us.
You need a local attorney to help you. Generally, if the IRS moves to redeem, the other lienholders will get notice. Your question is too complex to answer this specifically without looking at all of the paperwork.