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, and hurting others for one's own amusement won't earn much sympathy, either. 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. Contrast Poe's Law, when it really was just a joke but the satirical intention got lost.
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.
Films — Animated
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!"
Films — Live-Action
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!".
Done in Airplane! when Johnny in the control tower pulls a plug that cuts the runway lights as a prank.
Played for Drama in 42. 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.
Also Played for Drama, in Confessions. Shuya repeatedly jokes about killing his teacher's daughter, and shields any sincere emotion he might have with a "just kidding"
Revenge of the Pink Panther. After being presumed dead in a Mafia assassination, Inspector Clouseau returns home to find Cato has turned his apartment into a brothel (and the password to enter is his own name). After the inevitable comical misunderstandings, Clouseau indignantly shouts that he is a police officer. The bouncer promptly puts a switchblade to his neck, whereupon Cato rushes into the room.
Cato: No, don't kill him! I used to work for this man — he was only kidding! Tell them you were just kidding, boss!
Clouseau: Yes, I was just...kidding. Cross my heart and hope to (eyeing the knife) ...see you again some time.
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 dark, spoilery example from Warbreaker: Denth disguises Tonk Fah's genuinely sadistic and sociopathic behavior by playing it down and treating it like a joke, essentially passing him off as a Heroic Comedic Sociopath.
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 Toshiie to 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'!"
Frasier: Maris is quite clearly anorexic, but her condition is played entirely for laughs.
Rome. Julius Caesar sends Vorenus to bribe a former army colleague. Honest to a fault but loyal to his superior, Vorenus does so and reports back. Caesar comments that he didn't think the man would sell out so cheaply. "I must send you to handle all of my corruptions." Manipulative Bastard that he is, Caesar quickly sees how uncomfortable this makes Vorenus and says that he was only joking.
Mr. Brown, the first hotel guest on Fawlty Towers, does this after joking about needing a double bed and suggestively saying "hello" to Polly. His boorishness is justified when we learn that Mr. Brown is an undercover police officer, posing as a guest to investigate the phony Lord Melbury.
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.
Andrew "Dice" Clay has been known to trot out this defense, claiming his on-stage act is “satire.”
This defence was mercilessly parodied by Stewart Lee, who performed a nearly fifteen minute-long routine in which he mocked Richard Hammond and the other Top Gear presenters to the point that he states he wishes Richard Hammond had been decapitated and killed in his famous dragster crash in 2006. Lee then clumsily follows up the increasingly violent outbursts several times with "it's just a joke, like on Top Gear", demonstrating how lame an excuse this actually is.
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.
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.
EDI: I enjoy seeing humans on their knees.
EDI: That was a joke.
Dark Skye in Magick Chicks said "I was only playing"... after sending a pack of wolves to attack her schoolmate with the words "dinner time!". She was so erratic at this point that whether she meant it the first, second or both times is about equally likely.
A Running Gag in El Goonish Shive during the uniforms arc, was Tedd attempting to lighten the mood by saying something totally outrageous, and being geniunely confused that his friends weren't taking it in that spirit.
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.
Noob has the following situation. Sparadrap is a complete idiot, but has a brother among the elite players, that sometimes "borrows" his avatar to have an impromptu chat with his guildmates. Sparadrap's guildmate Omega Zell frequently insults his intelligence out loud, but is also trying to get on the good side of the game's elite. And one day, the insulting and the surprise avatar "borrowing" happened at the same time...
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'!
The 1940 Looney Tunes cartoon "You Ought To Be In Pictures" has Daffy cajoling Porky into quitting the cartoon business to be a legitimate actor in live films. When that proves disastrous, Porky returns, fishes his torn-up contract out of the trash and tells producer Leon Schlesinger he was only kidding. "April fool!"
In the Code Lyoko episode "Ghost Channel", Odd's boneheaded antics du jour had nearly gotten him devirtualized but he pulls off a spectacular return, earning him praise from his teammates. He then proceeds to tell Jeremie to sit back and let the real heroes take care of things. He starts laughing but then realizes that not only is no one else laughing but the other 3 people on Lyoko are glaring at him. He protests that he was just joking but it quickly becomes apparent the claim is falling flat.
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.
This happened between Thomas Edison and Nikola Tesla, and was the start of their infamous feud. Tesla told Edison he could greatly improve the efficiency of Edison's electrical generators and distribution. Edison remarked that if Tesla could do it, there was $50,000 in it for him. When Tesla succeeded and wanted his money, Edison told him, "You don't understand our American humor." Instead, Edison offered a pay raise that would have taken 96 years to equal the promised sum. Unsurprisingly, Tesla left the company instead.
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.
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 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?
The creator of the American Hentai game Tentacle Bento tried to claim it was a "satire" of misogynistic anime after people accused him of exploiting rape. Not surprisingly, nobody bought this excuse, and the game was removed from Kickstarter.