Pajamas were not bedclothes at first. They were everyday trousers.
The word is Persian pāy-jāma, a compound of pāy, "leg," and jāma, "clothing." It reached English by way of Hindustani, the language that gave us Hindi and Urdu. Etymonline defines the original garment as loose trousers tied at the waist, worn by Muslims in India.
English first recorded the word around 1800. Etymonline lists pai jamahs and pajamahs from 1800, and the Oxford English Dictionary's earliest evidence is dated 1801, according to Wikipedia. Merriam-Webster also gives 1801. The spelling varied for decades. The modern American spelling pajamas appeared by 1845, while British English kept pyjamas.
Wikipedia places the garment in a wider family of Indian and Iranian leg wear, so the Persian word fits the object. Merriam-Webster describes the early English sense as loose lightweight trousers once common in the Near East.
Europeans in India took to the garment, and especially to sleeping in it. Wikipedia says the trousers had earlier reached England in the 17th century as lounging wear called "mogul's breeches," but they fell out of fashion. Britons living in India in the 18th and 19th centuries then adopted them, and from about 1870 they became fashionable in Britain and the West as men's sleepwear.
That shift is the real story of the word. In South Asia pajama named a kind of trouser for any hour. In English it narrowed to the thing you wear in bed.
Pajamas also left a trail of shortened forms. Etymonline records jams from 1966, a clipping of pajamas, and notes that English had borrowed a related word, jam, "a kind of frock for children," from Hindi jamah in 1793. Both come from the same jāma "clothing" element.