When humour is used to excuse behaviour that others find offensive and inappropriate, you have just encountered a "Just Joking" Justification. Never mind that this seldom soothes the sting; the original barb hurt, after all. See the Real Life section below on the overlap between this trope and emotional abuse.
One source of humor which comedians have used as a reservoir of material, and a cause of this trope, are contemporary stereotypes, be they sexist, racist, homophobic, or otherwise. These can range a very wide gamut, from people getting upset at Michael Richards' (Seinfeld's Kramer) infamous n-word tirade at a heckler, to people getting upset when they don't realize Jon Stewart's jokes about Jewish conspiracies are actually deconstructive parodies (and heisJewish). Frequently features as an excuse for invoking Mars and Venus Gender Contrast.
You may also consider this as a form of justification for when an attempt to Cross The Line Twice results in Dude, Not Funny!. Not to be confused with I Was Just Joking. Also see Parody Retcon for when this is applied to entire works.
Detective Conan: Whenever Kogoro makes one of his off-the-wall, no-basis-in-fact accusations and it gets shot down, he will frequently backpedal by saying he was just joking.
Ai Haibara also always tag this to the end of anything that is actually real.
Darker Than Blackplayed with this. November 11 used to drop a comment somewhat disturbing for everyone present and add "Just joking" immediately. It's sufficiently annoying to other characters that it gets several Ironic Echoes throughout the series.
Bergelmir (Loki's brother) tries to pull one to cover his ass after attempting to trick Lucifer into being honor-bound to kill himself.
Bergelmir: It was joke, morningstar. A joke only. I've something of my brother's humor, after all.
Lucifer: Take me to the ship. Or I'll show you what makes me laugh.
In one version of The Pied Piper Of Hamelin, after the piper has gotten rid of the rats, the mayor claims he was only joking about the payment. If you've heard the story, you know how that turned out.
In the Adam Sandler film Jack And Jill, Jack (played by Sandler) has a mexican gardener whose only purpose in the movie is to make tons and tons of horrifically racist Mexican jokes ("We just got over the border!" "This is by son Juan, my other son Juan, and my daughter Juanita.") and follow them up with "I'm just KEEEEEEEEEEEEEEEEEEDING!".
The Spongebob Square Pants Movie makes fun of this briefly. After mind-controlling just about all of Bikini Bottom, Plankton is arrested. C'mon, he was just kidding.
Aladdin and the King of Thieves. Genie has made what could be seen as an extremely insensitive joke (that Aladdin isn't really required for a wedding to happen). "It's a joke! (deadpan) I do that."
Mother Gothel from Tangled uses this quite frequently when talking to Rapunzel.
Gothel: "Ah, Rapunzel, how you manage to [pull me up the tower with your hair] every single day without fail... it looks absolutely exhausting, darling." Rapunzel: "Oh, it's nothing." Gothel: "Then I don't know why it takes so long! Ohohoho, darling, I'm just teasing!" Rapunzel: "Eh-heh... So, mother, as you know, tomorrow is a very big day-" Gothel: "Darling, look at that mirror. You know what I see? Is see a strong, confident, beautiful young lady. Oh, look, you're here too. Ahahahaha! I'm just teasing, stop taking everything so seriously!"
Done in Airplane! when Johnny in the control tower pulls a plug that cuts the runway lights as a prank.
Played for Drama in Forty Two. After Ben Chapman gets backlash for his racist diatribe against Jackie Robinson, he defends himself by claiming that it's tradition to use derogatory slurs to distract opposing players.
Romance of the Three Kingdoms abuses this trope horrendously. If a lord says something to upset a favoured retainer, they usually claim it was a joke. Even if the statement that upset the person wasn't funny at all.
In The Screwtape Letters, Screwtape discusses this as a method of tempting a person to be a jerkass. After all, they might not realize their jerkassery is hurtful if they—and better yet, all their friends—see it as just a joke.
A rare example without overtones of racism or outright cruelty: on The Dick Van Dyke Show there was an episode where Jerry Helper (the next door neighbor) was going around saying The Alan Brady Show — on which Robert Petrie (Dick Van Dyke) worked — wasn't funny. He told Rob he was "just kidding" but kept on doing it. Rob was quite upset.
In Toshiieto Matsu, Oda Nobunaga attempts to pass off a particularly tasteless move (displaying the gold-plated skull of his sister's dead husband, and ordering her and her new fiancee to drink from the skullcap) on his part as a joke.
Larry David in Curb Your Enthusiasm makes frequent use of this (usually as a last resort if he needs something from whoever he offended).
In the Hannah Montana episode "Judge Me Tender", a Simon Cowell parody character issues one of these, then immediately retracts it, after saying both Hannah and the judge for whom she was filling in were boring.
An episode of Malcolm in the Middle has a new neighbor boy spreading malicious lies about Malcolm, turning the entire neighborhood against him. When Malcolm confronts him, the kid says, "Hey, it's just a joke. Lighten up," and continues doing it.
Michael Scott often finds himself caught in these after an egregious faux-pas or a joke in really bad taste.
Scrubs- the Janitor brings a cage to a baby shower(for the baby). When he gets weird looks he then plays it off like he was just joking and really brought it just in case the child gets a puppy later on. We later see him on the phone "No, Ma, 'baby cage, playpen' is not like 'tom-ay-to, tom-ah-to'!"
The phone rings, it's early, it's seven o'clock. He says I'm sorry that I woke you but I just had to talk. You know, last night, remember when I tried to choke you? I didn't mean it, I was drunk, it was only a joke.
Get Fuzzy: Bucky occasionally says "I'm just sayin'..." after insulting humans and dogs, eventually prompting this exchange:
Satchel: "Bucky, you're a jerk!" Bucky: *Stunned Silence* Satchel: "Hey, I'm just sayin'!"
During one episode of the Big Finish Doctor Who audio dramas, Peri and Erimem are currently posing as two princes who are supposed to be dead, and are as far as they know, stranded due to a temporal mishap. Erimem suggests suicide (to save the timeline) is the only answer, but then pretends she was just joking when hearing Peri's shocked response.
Stand Up Comedy
Some comedians, such as Russell Peters, have received criticism for this.
Michael Richards stepped far afoul of this one when a heckler got him so flustered he went on a racist tirade that was so Dude, Not Funny! people started walking out. He later said he was trying to shock the hecklers into silence by Crossing The Line Twice, but most people think he forgot to cross it back.
Subverted by Jeff Foxworthy in one of his stand up acts. After making a joke about having cats stuffed by a taxidermist...
Jeff: Did I offend someone in the audience? It's a joke. I have two cats. (beat) One on either side of the fireplace.
However, he played it straight on a later album. After making a joke that he feared could be misconstrued as homophobic, he launched into a series of jokes on how gay people and married people are Not So Different.
In Mass Effect 2, your shipboard AI, EDI, will attempt to crack a few jokes, but ends up append these comments with the phrase "That was a joke" when the people listening don't find it funny, or are afraid she's gone rogue.
The yandere family to Donnie in Demo Reel. They held him captive, planned to keep him through feeding him muscle relaxants, think he's kidding around about his mother dying, and keep on gleefully reminding him about his tragic past. When he's free and pissed, they try to get out of it by mewling "we were just having a little fun!". As one might expect, he doesn't let them off the hook.
Krusty the Clown in the episode "Like Father, Like Clown" deconstructs this after an offensive bit by outright saying, "When you look at me like that, it's a joke."
Following Sideshow Bob's Heel Face Turn in "Brother From Another Series", he remarks that he still just might have the urge to kill Bart Simpson, only to quickly add "Joking, joking!" when Bart gets frightened.
Bart's chalkboard punishment for the episode "Homer the Great" reads "Adding 'just kidding' doesn't make it okay to insult the principal".
Casinko, an episode of Sealab 2021 contains a heavily referenced song entitled "We're Only Joking". 
When Deputy Dawg starts the wedding of Mighty Mouse and Pearl Pureheart (1988 episode "Mighty's Wedlock Whimsy") with "You have the right to remain silent," Sourpuss calls him on it:
Sourpuss: No, dog! That's da wrong speech! Dis is supposed ta be a weddin'!
Deputy Dawg: (laughs goofily) Just checkin'!
This is one of the typical excuses offered for the emotional and psychological torment inflicted by bullies.
More generally, it seems to have become acceptable shorthand (at least among the sort who assume that anyone else would do the same) for "I Lied."
'Only Teasing' seems to be a varient of this, frequently with the implication that the victim is being 'too sensitive.'
Comedian Andrew O'Neill references this trope in his video I'm not being racist but... . In summary, he demonstrates that the phrase renders anything suspicious and disturbing.
Humorist and U.S. Senator Al Franken (D–Minnesota) has used the term "kidding on the square" to refer to a type of "joking" that's subtly, or not-so-subtly, serious in its intent.
"Fuck 'em if they can't take a joke" is part of the SubGenius dogma, but trying to justify it as "just a joke" is one of the few sins. The inversion of this trope is Ha Ha Only Serious.
A common story (most likely apocryphal) says that Thomas Edison commissioned Nikola Tesla for a considerable sum and, when Tesla finished the work and asked about this mythical payment Edison had mentioned, just told him, "You don't understand our American humor." This happened before they were archenemies, and when Tesla was still working for Edison's company. Edison supposedly had promised a large sum for a person who could solve a specific problem in the design of a component, but after Tesla had done just that, Edison noted that without a written contract such promise is worthless. Tesla then asked for a relatively small raise on account of being Edison's most useful employee. Edison refused. Tesla resigned.
Since this type of humor has become more popularized and people think they can hide behind it, it's been fairly common for newspaper columnists, for instance, to write straight editorials that are blatantly bigoted and then when they're called out say, "It was obviouslymeant to be satire!"... only to completely fall down when the time came to explain exactly what they were satirizing and why it deserved it.
It is quite easy to fool people by beginning insults with "No offense, but..." Smart people would get Fridge Logic immediately. The key word here is "but."
Infamously used by Jack Thompson to back out of his challenge to video game developers, when they actually created a violent video game to satirize themselves like he challenged them to. When he wouldn't make the donation to a charity as promised, the folks behind Penny Arcadefooted the amount in his name◊. For which he sued them for harassment, and tried to have them arrested.
Sarah Palin said that Rahm Emanuel's usage of the word "retarded" was offensive. When a controversy erupted, Rush Limbaugh chipped in and said "In today's politically-correct society, there's a big deal if you call a retard, retard". Palin ranted against Emanuel and said that he had offended "all of God's children with mental disabilities", but then said that Limbaugh's comment was OK because it was "satire."
Stephen Colbert then joined in, saying that his comments were just satire, and said on TV: "Sarah Palin, you're a fucking retard."
One episode of Family Guy had a Lawyer-Friendly Cameo of an Alaskan governor's mentally handicapped child. Sarah Palin took offense to this, and replied with her usual reaction. The actress who played the character is herself mentally handicapped, and responded to Sarah Palin with "my parents taught me to have a sense of humor about my life, instead of parading me around the country for pity votes."
South Park seems to get immunity from criticism (at least since it has become popular) for piling all kinds of political incorrectness and Black Comedy into what is ostensibly a kiddie cartoon show while older cartoons with material only about half as offensive (such as the infamous "Censored Eleven") are never shown on TV (sort of a reverse Grandfather Clause, if you think about it). The justification is apparently something along the lines of "Well, Trey Parker and Matt Stone don't really believe in that stuff and are actually mocking it, so it's okay." Well, how do you know that older animators didn't do likewise?
Very common excuse for misogynistic or otherwise discriminatory remarks, like "jokingly" saying that you'd like to rape a woman, and then blaming her for overreacting/being hysterical/having no sense of humour when she calls you out on it.
Stewart Lee, in this article, deconstructs this attitude and the recent reputation it has for symbolising a comedian as being 'brave' and 'non-PC'. He does it humourously, but underneath the humour is the point that sometimes this defence is sometimes just used by the comedian to handwave being a bullying jerk.