I see that you've repeated the initial story. Here's a suggested response to the awkward situation:
Apologize: Begin by expressing your regret for the unexpected encounter. "Oh, I'm so sorry! I didn't know you were here, and I wasn't expecting company."
Explain briefly: Acknowledge your lack of appropriate attire, but keep it light and brief. "I just got home from work and hadn't changed yet."
Make amends: Offer a solution to alleviate the awkwardness. "Please, make yourself at home. I'll just be a moment, and then we can catch up properly."
Follow through: Excuse yourself to put your pants back on and return to continue the conversation in a more appropriate manner.
By addressing the situation directly, you can help put both of you at ease and move past the awkwardness.