Kontsevich's formality theorem states that there exists an L∞ quasi-isomorphism from the dgla Tpoly•(M) of polyvector fields on a smooth manifold M to the dgla Dpoly•(M) of polydifferential operators on M, which extends the classical Hochschild–Kostant–Rosenberg map. In this paper, we extend Kontsevich's formality theorem to Lie pairs, a framework which includes a range of diverse geometric contexts such as complex manifolds, foliations, and g-manifolds (that is, manifolds endowed with an action of a Lie algebra g). The spaces tot(Γ(Λ•A∨)⊗RTpoly•) and tot(Γ(Λ•A∨)⊗RDpoly•) associated with a Lie pair (L,A) each carry an L∞ algebra structure canonical up to L∞ isomorphism. These two spaces serve as replacements for the spaces of polyvector fields and polydifferential operators, respectively. Their corresponding cohomology groups HCE•(A,Tpoly•) and HCE•(A,Dpoly•) admit canonical Gerstenhaber algebra structures. We establish the following formality theorem for Lie pairs: there exists an L∞ quasi-isomorphism from tot(Γ(Λ•A∨)⊗RTpoly•) to tot(Γ(Λ•A∨)⊗RDpoly•) whose first Taylor coefficient is equal to hkr∘(tdL/A∇)12. Here the cocycle (tdL/A∇)12 acts on tot(Γ(Λ•A∨)⊗RTpoly•) by contraction. Furthermore, we prove a Kontsevich–Duflo type theorem for Lie pairs: the Hochschild–Kostant–Rosenberg map twisted by the square root of the Todd class of the Lie pair (L,A) is an isomorphism of Gerstenhaber algebras from HCE•(A,Tpoly•) to HCE•(A,Dpoly•). As applications, we establish formality and Kontsevich–Duflo type theorems for complex manifolds, foliations, and g-manifolds. In the case of complex manifolds, we recover the Kontsevich–Duflo theorem of complex geometry.