Let’s say you’re in the gym, and you’ve left your keys next to some equipment, intending to pick them up later. Someone brings you the keys, thinking you had dropped them by accident. You don’t explain that you knew where they were; you simply accept them graciously and say “thanks”. (If the person happens to be a beautiful woman, you’re permitted to expand on this a bit, such as “thanks, I really appreciate that, would have been suicidal if I had lost them.”) In such a case, it is the intention, rather than the fact, that you are thanking someone for.
If someone is attempting to do you a favor that you really don’t want, like setting you with their sister in law, who you happen to know is a mean, man-hating bulldog of a woman, you should say, “thanks, I really appreciate your thinking of me, but that’s probably not a good idea.” Don’t cave in if they insist; “No, really, I’ve done that a million times, and it never works out for me.”
In terms of giving favors, keep in mind that its very hard to know what other people really want. So often, a well-intended favor is not something welcomed by the recipient. But if you insist, give it a simply and unobtrusively as possible, giving the recipient as much opportunity as possible to decline the offer. And if they do decline, beyond what is culturally required for good manners, don’t insist. You should strive to make it appear as if they’re doing you a favor to let you do them a favor.