The seven oceans were formed by the impressions of the chariot wheels of Priyavrata, the eldest son of Swayambhuva Manu.
From Śrīmad-Bhāgavatam Skandha 5, chapter 1:
While so excellently ruling the universe, King Priyavrata once became dissatisfied with the circumambulation of the most powerful sun-god. Encircling Sumeru Hill on his chariot, the sun-god illuminates all the surrounding planetary systems. However, when the sun is on the northern side of the hill, the south receives less light, and when the sun is in the south, the north receives less. King Priyavrata disliked this situation and therefore decided to make daylight in the part of the universe where there was night. He followed the orbit of the sun-god on a brilliant chariot and thus fulfilled his desire. He could perform such wonderful activities because of the power he had achieved by worshiping the Supreme Personality of Godhead. [5-1-30]
So, he decided to circumambulate the Sun-god in his chariot.
When Priyavrata drove his chariot behind the sun, the rims of his chariot wheels created impressions that later became seven oceans, dividing the planetary system known as Bhū-maṇḍala into seven islands. [5.1.31]
By doing this, oceans were formed. These oceans divided the earth into seven islands.
The names of the islands are Jambū, Plakṣa, Śālmali, Kuśa, Krauñca, Śāka and Puṣkara. Each island is twice as large as the one preceding it, and each is surrounded by a liquid substance, beyond which is the next island. [5.1.32]
King Privarta declared his sons as the lords of these islands. The names of the oceans and their respective lords are given in the next verse.
jaladhayaḥ sapta dvīpa-parikhā ivābhyantara-dvīpa-samānā ekaikaśyena
yathānupūrvaṁ saptasv api bahir dvīpeṣu pṛthak parita upakalpitās teṣu
jambv-ādiṣu barhiṣmatī-patir anuvratānātmajān āgnīdhredhmajihva-
saṅkhyenaikaikasminn ekam evādhi-patiṁ vidadhe.
The seven oceans respectively contain salt water, sugarcane juice, liquor, clarified butter, milk, emulsified yogurt, and sweet drinking water. All the islands are completely surrounded by these oceans, and each ocean is equal in breadth to the island it surrounds. Mahārāja Priyavrata, the husband of Queen Barhiṣmatī, gave sovereignty over these islands to his respective sons, namely Āgnīdhra, Idhmajihva, Yajñabāhu, Hiraṇyaretā, Ghṛtapṛṣṭha, Medhātithi and Vītihotra. Thus they all became kings by the order of their father. [5.1.33]
Hence, King Priyavrata is the reason for the formation of the seven oceans including the ocean of milk.
But the churning of the ocean took place due to the curse pronounced by Sage Durvāsas on Indra. It was done to restore the treasure of the devatas.
The incident is explained in detail in this chapter of Viṣṇu Purāṇa.
Sage Durvāsas gives a garland made of heavenly flowers to Lord Indra. He gives to his favourite elephant Airāvata and wears it on its brow. whose eyes were dim with inebriety, That elephant attracted by the smell throws it down. Knowing this, Sage Durvāsas became angry and pronounced a curse on Indra.
Now, fool, for that thou hast not infinitely prized the garland that I gave thee, thy sovereignty over the three worlds shall be subverted. Thou confoundest me, Śakra, with other Brahmans, and hence I have suffered disrespect from thy arrogance: but in like manner as thou hast cast the garland I gave thee down on the ground, so shall thy dominion over the universe be whelmed in ruin. Thou hast offended one whose wrath is dreaded by all created things, king of the gods, even me, by thine excessive pride.
Indra asks for forgiveness but the sage leaves Amarāvati. In this way, all the possessions were gone. On the advise of Lord Hari, devatas and asuras churned the ocean.