- National Comics (the predecessor to DC) made Superman comics
- Fawcett Comics made Captain Marvel comics, inspired by Superman
- Fawcett's Captain Marvel and spinoffs became more popular than Superman
- National sued Fawcett, and in the lawsuit it came out that some Captain Marvel stories were direct copies of Superman stories with the characters swapped
- Fawcett settled the lawsuit and shut down Captain Marvel production
- Over a decade later, Marvel Comics started publishing their own Captain Marvel (a different character aside from the name) and got the trademark on the name
- DC, as successor to National, licensed and later bought out the Fawcett Captain Marvel, then added him to their own universe alongside Superman
That's how you get to a Marvel movie titled "Captain Marvel" featuring Captain Marvel, and a DC movie titled "Shazam!" featuring the other Captain Marvel, even though the now-DC one came first.
There's a fork in this history arising from the British publishing of Captain Marvel - i had a go at summarising the wikipedia entry [1], but gave up, because it's too bananas.
The gist is that when Captain Marvel was shut down, the British publisher came up with a knockoff called Marvelman, who then went through a comparable set of rights battles and name changes, becoming Captain Miracle, then Marvelman again, then Miracleman, then Man of Miracles (when eventually bought up by Marvel, weirdly enough).
Along the way, there was a really disturbing run in the '80s written by Alan Moore, which was out of print for years because of the rights battles, and also bits from Neil Gaiman and Grant Morrison. Oh, and a villain called Young Nastyman, who turns up in a Tenacious D song.
Man of Miracles is actually another tangent, where Todd McFarlane, the guy who make the Spawn comics, incorrectly believed he'd purchased the Miracleman rights and featured the character in his comic, then had to pivot and retcon the character into something else entirely when sued by a new company formed by Gaiman to arrange republishing of the Moore/Gaiman/Morrison stories.