“Mary asked me to give this box to you.” To which one replies, “Thank you. You’re a gentleman and a scholar.”

The non-sequiturial nature of the reply dovetails with the tinkle-in-the-eye part of the phrase. I've heard it used often with the mock-flattering, mock-self-aggrandizing "You're a gentleman and a scholar, and there's only a few of us left!"