I’m not 100% on India-Pakistan but I’m pretty sure the Kashmiris didn’t want to be apart of either was the point, but for security reasons it joined India until there was a ceasefire and there have been smaller battles ever since.
The same way Sri Lankan and Bangladesh became their own countries. Kashmir was technically independent until recently.
They drove out a bunch of Hindus through violence which is ethnic cleansing.
Before 1947, about 60% of "British India" was ruled directly by the British while the other 40% consisted of "princely states" of various sizes (some the size of cities, some fairly large). These states had their own rulers who were in charge of the state's internal affairs, but they were basically vassal states of the British Empire, and the British still managed their foreign affairs.
When British India was to be divided (because the Muslim leadership demanded their own country), the parts directly ruled by the British were to be divided along religious lines, and it was (to some extent) left to the rulers of the princely states to decided what they wanted to do. Here's what happened:
1. Of the parts ruled directly by the British, each province became a part of Pakistan if it had a Muslim majority and part of India otherwise. There were two exceptions: Punjab and Bengal, which had large non-Muslim minorities and religious populations that were unevenly distributed. It was decided that Punjab and Bengal would be partitioned. These partitions resulted in forced population exchanges brought about by ethnoreligious cleansing. In Bengal, there were still substantial minorities left on both sides of the border after the partition. East Bengal became part of Pakistan and later became Bangladesh after getting independence from Pakistan in 1971 (a lot of Hindu refugees who were left in East Bengal after 1947 moved to India by the end of the Bangladesh liberation war). In Punjab, virtually all Sikhs and Hindus in the western part were killed or escaped to the east (where they were resettled) and in virtually all Muslims in the eastern part were killed or escaped to the west (where they were resettled).
2. Basically all of the princely states decided to join India or Pakistan based on the obvious factors (religious composition and geographic proximity to India or Pakistan). The exception was Kashmir, which had a Hindu ruler and a Muslim majority. The ruler first wanted to remain independent, viewing joining either India or Pakistan as a bad choice, but eventually decided to join India. A war in 1947-48 resulted in a division of the territory, and disputes over this territory have basically fueled hostilities between Pakistan and India ever since.