Anti-inflammatory medications work by inhibiting the production of inflammatory mediators like prostaglandins. NSAIDs, for example, block the enzyme cyclooxygenase (COX), which is involved in the production of prostaglandins. By reducing the levels of these mediators, these medications help alleviate pain and reduce swelling.