Prior to Cyrus the Great, a Persian, the Medians were overlords of the Persians and other tribes in what is now western Iran. Cyrus overthrew the Median king, Astyages, and founded the Achaemenid Empire, with the Media as part of the empire. Cyrus went on to conquer Babylon in 540 BCE and soon after allowed the Jews to return to Jerusalem, for which he is mentioned extensively in 2 Chronicles, Ezra and Isaiah (Second Isaiah) by the grateful Jews.
Cyrus was killed in battle in about 530 BCE, in Central Asia, and was succeeded by his son, Cambyses. In 522 BCE, while Cambyses was campaigning in the west, a usurper, Bardia or Smerdis, occupied the throne. He was quickly overthrown by Darius, who, although a Mede, was a military commander in the Persian empire. Darius I ruled Persia from 522 until 486 BCE and was succeeded by his son Xerxes (known as 'Ahasuerus' in the Bible).
The Cambridge Ancient History Volume III Part 2, page 395, says the Book of Daniel is generally considered to be a historically unreliable compilation of the second century BCE. Two of the historical errors in the Book of Daniel were to describe to Darius "the Mede" as the son of Xerxes and as the king who conquered Babylon, at the age of sixty two (18 years before he overthrew Smerdis and really became king):
Daniel 5:31: And Darius the Median took the kingdom, being about threescore and two years old.
An alternative view
Sara Raup Johnson (Historical Fictions and Hellenistic Jewish Identity, page 58) provides the background to another interesting explanation of "Darius the Mede". She tells us that Josephus, writing late in the first century CE, was baffled by the reference to "Darius the Mede", who ruled before Cyrus according to the Book of Daniel. Jewish Antiquities Book 10 closely follows the Book of Daniel, but Josephus is unable to identify another historical Darius who would preserve Daniel as a book of history. Johnson says that Josephus came up with a characteristically ingenious solution. Josephus wrote that Darius the Mede was a son of Astyarges the Mede, maternal grandfather of Cyrus "but was called another name among the Greeks", and that he assisted Cyrus in the overthrow of Babylon. She says (Page 59) that Josephus stands at the head of a line leading to modern scholarship's vain attempts to make historical sense of texts never written as pure history.
Steven Anderson (https://www.academia.edu/9787699/Darius_the_Mede_A_Reappraisal) looks at some options for 'Darius the Mede' to have taken part in the conquest of Babylon. A critical assumption in this dissertation is that 'Darius the Mede' either existed or he did not; Anderson does not consider the possibility that Daniel's author displaced King Darius I in time. At page 51, he acknowledges that mainstream scholarship says that the Book of Daniel was a compilation of the second century BCE, at the same time cautiously raising his own doubts on this. The problem is that if the Book of Daniel was a late, pseudepigraphical work, we have no reason to believe it to be historically accurate. Anderson acknowledges that Jerome cited Josephus, whom Anderson acknowledges (page 5) as appearing to have been the first to identify Daniel’s Darius the Mede with Xenophon’s Cyaxares II. Thus, Josephus was the only independent source for this claim, and he is already well known for taking a biased view of history when it suited.