The classic way, which I have actually seen used more in Shadowrun than in D&D, is to convince someone else that the lie is true, and then have that person tell the people who can Detect Lies. Because the middleman believes the lie is true, the power doesn't show up that they are lying.
http://www.d20srd.org/srd/spells/discernLies.htm
That works for outright detection. I don't know about the rest of your questions. But that first one seems pretty easy. I feel like I'm missing some aspect you are really trying to ask.
I've seen Sense Motive be used as a poor man's lie detector, though I have an appreciation that it isn't supposed to be used that way. Or more specifically, it should be used for "he is being secretive" not for "he is lying" or "[specific thing] is a lie." But then again I've seen a lot of skills used differently than they were written and I quite love that.
Thanks for your thoughts. I'm often torn, in almost every edition, of just how much to give the players in regard to agency when making rolls themselves to utilize skills or spells to divine the truth in situations, from NPCs, etc. I'm constantly rethinking how it works.
You can have a shapeshifter take your place. That person then correctly denies having done what you have done. Doesn't fool Detect Thoughts though. And if the court is thorough, they will probably check for it. You can try to switch after your identity has been ascertained but before testimony.
You can also try a simulacrum instead of the shapeshifter.
Also a saving throw helps. Magic resistance might, as well as Glibness.