It’s complicated.
DC Comics created Superman in 1938 and he was a big success. So a lot of other comic book companies created similar characters. One of these companies was Fawcett, which created the character of Captain Marvel in 1939.
There were differences in the characters’ non-hero identities. Superman was an alien who came to Earth as a baby when his own planet was destroyed. He grew up in Kansas under the identity of Clark Kent. Captain Marvel was a young boy named Billy Batson, who was told a magic spell (he says the word “Shazam”) that could transform him into an adult superhero. But as superheros, the two characters had very similar powers and carried out very similar stories.
Captain Marvel was also a major commercial success but DC filed lawsuits against Fawcett over character infringement. Sales declined and Fawcett retired the character in 1953.
DC Comics licensed the rights to Captain Marvel from Fawcett in 1972 and bought them outright in 1993. So Captain Marvel now appears as a DC character alongside Superman.
And this was the simple part of the story.
Marvel comics led a revival of comic book in the early sixties, introducing a whole line of new characters (such as the Fantastic Four, the X-Men, Thor, Iron Man, Spiderman, etc). They created a character named Captain Marvel in 1967. Note that this was during the period between 1953, when Fawcett stopped using the character, and 1972, when DC acquired the rights. So nobody much cared if a different character named Captain Marvel existed.
After DC revived the original character, there were issues over the use of the name by Marvel’s character. It was decided that Marvel could keep the name as long as they didn’t let it fall into disuse. DC was also allowed to use the name in relation to the original character. But Marvel had the rights to use Captain Marvel as the title of a comic book series so DC began publishing stories about their Captain Marvel under the title Shazam!. This led to some confusion as people thought the character was named Shazam.
So Marvel makes sure to publish new comic books with a character named Captain Marvel at least once every two years so they retain their rights. But they haven’t consistently used it for a single character.
The original Marvel Captain Marvel was an alien named Mar-Vell, who came to Earth with super powers. This character died a dramatic death due to cancer in 1982 (it was considered a major moment in comic book history to have a superhero character die from natural causes and stay dead.)
But Marvel had to make sure the name stayed alive even after the character died, so they introduced a new character named Monica Rambeau, a police officer who acquired super-powers and joined the Avengers. She used the name Captain Marvel for several years.
There were also several other characters who were successors to the now-dead Mar-Vell who also used the name Captain Marvel after Rambeau stopped using it. None of them were very significant but they served to keep the name in use.
Meanwhile, there was another Marvel character named Carol Danvers, who was introduced in 1968. She acquired super-powers from the original Mar-Vell and adopted the name Ms Marvel. As Ms Marvel, she was a fairly significant character for many years. In 2012, she decided to change her name to Captain Marvel in honor of Mar-Vell. (Another younger woman named Kamala Khan adopted the name of Ms Marvel and made comic book history by being the first major openly Muslim character in comic books.) Danvers is the current Captain Marvel in the Marvel universe and she is the protagonist of the upcoming MCU movie.
With Marvel’s character having eclipsed the original one and not being able to use the name as the title of a series, DC finally decided to throw in the towel and renamed their Captain Marvel as Shazam in 2011.