Egregious is a compliment that went sour. For its first half-century in English, calling someone egregious was high praise.
The Latin source was egregius, "distinguished, excellent, extraordinary." It came from the phrase ex grege: ex, "out of," and grege, a form of grex, "flock" or "herd." Someone egregious rose above the flock. Etymonline notes that the Latin word never had a bad sense. It simply meant exceptional.
English took the word in the 1530s with that admiring meaning: distinguished, eminent, renowned. Christopher Marlowe could still write "Egregious viceroys of these eastern parts" in Tamburlaine, in 1590.
The trouble was irony. Michael Quinion of World Wide Words dates the change to about 50 years after the word arrived. People began using it jokingly for someone who stood out from the crowd in a bad way. The joke works the same way as calling a blunder "brilliant." In Shakespeare's Cymbeline, from 1611, Posthumus says "Egregious murderer," and there is no praise in it.
For about 200 years the two meanings ran side by side. By the 19th century, Quinion writes, the strongly negative sense had won. Today an egregious error is one so bad it is conspicuous, and the old sense of "eminent" is rarely met outside old books.
Italian kept the original meaning. The salutation egregio still carries the respectful sense, so the same Latin word now points in opposite directions in two languages.
The flock behind the word has relatives across English. Grex comes from a doubled form of an old root meaning "to gather." Gregarious first described animals that live in flocks, in the 1660s, and only in 1789 was it used for sociable people. Congregate is from Latin congregare, "to herd together, collect in a flock." An egregious person, in the original sense, was the one who stepped out of that crowd for the right reasons.