This is a favorite trope of Hollywood submarine movies:
But has it ever really happened?
This tactic is a common feature of media depicting ASW in WWII - as additional examples, it shows up in Montsarrat's The Cruel Sea and Forester's The Good Shepherd, in both cases from the perspective of an escort. There is a reason for this: it was a commonly used tactic, albeit one that was sometimes inadvertent.
During WWII, escorts and aircraft had few ways to tell whether or not a submarine had been sunk. Sonar (or ASDIC) was relatively limited, and often blinded by the noise of an attack. Aircraft, of course, had no real access to it. As a result, observations of oil or other debris floating to the surface was often the only indication that a successful attack had been made. This was never fully effective, as it could be confused by decoys or leaks from damaged submarines.
During the night of the 19th-20th February 1940, the British submarine L23 was on patrol off the coast of Norway when she was attacked by three German minesweepers. The minesweepers made a heavy depth-charge attack, which brought a series of air bubbles to the surface, followed by a large oil slick. They assumed that this indicated the sinking of the submarine, and made no further attacks. In actuality, one of L23's fuel tanks had been breached. The submarine survived, and limped home with severe damage. For more deliberate examples, we can look to two case studies in late 1939. The first came on October 30th 1939, when SS Bronte was torpedoed by U-34. Bronte was travelling in a convoy escorted by the destroyers Walpole and Whirlwind, who immediately counterattacked the submarine. They gained a strong ASDIC contact on the U-boat and attacked with depth charges. Oil and air bubbles rose to the surface afterwards, and they concluded that the submarine had been sunk. In fact, U-34 survived, and would sink the SS Malabar a couple of days later. The Royston Grange was attacked in convoy by U-28 on the 25th November 1939. Again, the escorting destroyers counterattacked, dropping a total of fifteen charges in four attacks. They observed oil and water rising to the surface after the attacks, and concluded that they had at least damaged the submarine. In reality, the submarine had survived, with no evidence of damage; she mined Swansea harbour shortly afterwards and completed her patrol successfully. There were many other such cases of the tactic being used, to greater or lesser effect.
The tactic was refined over the course of the war. Royal Navy sources, based on interrogations of German survivors, describe U-boats being fitted with special oil charges. These were capable of being released while the sub was submerged, to create a convincing oil slick. This considerably simplified the process of creating such a decoy, and could be used without sacrificing a torpedo tube. They also mention Hitler calling for the development of 'a kind of torpedo which could explode on the surface, eject oil, and cause air bubbles to appear'.
The frequent use of these kinds of decoys had a significant effect on Allied escort commanders. They were often suspicious that any release of oil was actually a decoy. On the 20th August 1940, U-413 was attacked by three British destroyers, Forester, Vidette and Wensleydale. A successful attack by the Hedgehog spigot mortar of Vidette sank the submarine, with her chief engineer managing to escape from the boat on the bottom. He was picked up, along with several fragments of the boat's interior. Despite this evidence, the captains of Forester and Vidette were unconvinced, and wanted to stay in the area until the U-boat was forced to surface by lack of air.
But has it ever really happened?
It has. The earliest known case is the WWI German submarine UC-44, which fired oil and debris from a torpedo tube, successfully deceiving its attackers into thinking it sunk and stopping their depth charge attack. This took place on 15th February 1917. UC-44 proceeded to escape, and the same tactic was used by other German submarines during the war. For example, UC-48 appears to have fired wooden debris after being severely damaged in a depth charge attack on 20th March 1918; she avoided being sunk (but having lost much fuel due to damage from the attack, could only make it to Spain, where she and her crew were interned for the duration).
Other navies adopted the tactic, too. The official US submarine doctrine of 1944 noted the value of this type of deception:
In the past, deliberate oil slicks and blowing of debris out of torpedo tubes or garbage ejectors has been an effective means of shaking off pursuit.
The wider availability of sonar in WWII, and the deception tactic itself being known, made it less effective in WWII than it had been in WWI. Still, it continued to be used. One of the last known examples of the wartime use of this was U-853, sunk off Rhode Island on 6th May 1945. U-853 was depth charged, and oil and debris including wood and a captain's hat were spotted. However, sonar showed that U-853 was still moving, and attacks continued, sinking her. She was sunk with no survivors, so it is only an assumption - but a reasonable one, considering that she was still moving after the debris was seen - that she fired debris to deceive her attackers.