Mechanic's liens in their modern form were first conceived by Thomas Jefferson, to encourage construction in the new capital city of Washington. They were established by the Maryland General Assembly, of which the city of Washington was then a part. However, it is not likely that Jefferson single-handedly dreamed up the idea.
At the time Jefferson promoted the law, a lien-like privilege already existed in civil law countries like France, the Dutch Republic and Spain, with some laws even tracing their roots to the Roman Empire. And since control of Louisiana had passed between the French and Spaniards, and had largely adopted the French Napoleonic Code, there was a similar privilege concept in that territory.
It seems highly likely that the legislators working on the first U.S. Mechanic Lien laws had knowledge of, and referenced these civil law concepts. Nevertheless, the United States version was original, as it gave builders a more robust right into the land itself (versus just the improvement’s value).
With respect to real property, mechanic's liens are purely statutory devices that exist in every state (although in California, as noted below, they have a constitutional foundation). The reason they exist is a legislative public policy to protect contractors. More specifically, the state legislatures have determined that, due to the economics of the construction business, contractors and subcontractors need greater remedy for non-payment for their work than merely the right to sue on their contracts. In particular, without the mechanics' lien, subcontractors providing either labor or materials may have no effective remedy if their general contractor is not sufficiently financially responsible, because their only contractual right is with that general contractor.
Without the mechanic's lien, the contractor would have a limited number of options to enforce payment of the amounts owed. Further, there is usually a long list of claimants on any failed project. To avoid the specter of various trades, materialmen and suppliers attempting to remove the improvements they have made, and to maintain a degree of equality between the various lienors on a project, the statutory lien scheme was created. Without it, Tradesperson A may try to "race" Supplier B to the courthouse, the project site or the construction lender to obtain payment. Most lien statutes instead mandate strict compliance with the formalized process they create in return for the timely resolution and balancing of claims between all parties involved - both owners and lien claimants.