A Mandarin was a bureaucrat in imperial China, and also in Feudal Vietnam where the system of Imperial examinations and scholar-bureaucrats was adopted under Chinese influence.
The English term comes from the Portuguese mandarim or Dutch mandarĳn, from Malay məntəri, from Hindi mantrī, from Sanskrit mantrin (meaning councilor or minister. The term is also used to refer to the northern spoken variety of Chinese because it was the language used among officials during the Ming and Qing dynasties.
An alternative theory is that the term comes from the Chinese phrase "Mandaren" (满大人), meaning a "Manchu official". However, since there is no direct evidence supporting this hypothesis, and since the word "mandarin" is attested in the early 16th century before the establishment of the Qing Dynasty, this is deemed unlikely by linguists.
In the West, the term "Mandarin" is associated with the concept of the scholar-official, who immersed himself in poetry, literature, and Confucian learning in addition to performing civil service duties.
For around 1300 years, from 605 to 1905, Mandarins were selected by merit through the extremely rigorous imperial examination.
China has had civil servants since at least the Zhou Dynasty. However most high ranking positions were filled by relatives of the sovereign and the nobility. It was not until the Tang Dynasty when the final form of the mandarin was completed with the replacement of the nine-rank system. The mandarins were the founders and core of the Chinese gentry. The mandarins were replaced with a modern civil service after the fall of the Qing Dynasty.
The wardrobe of a mandarin during the Qing Dynasty included Manchu official headwear and a mandarin square. The office and residence of a mandarin was a yamen.
In modern English, "Mandarin" is also used to refer to any (though usually a senior) civil servant, often in a satirical context, and particularly in the United Kingdom and Commonwealth countries.