| Exhibit 5.1 ATTORNEYS AT LAW 777 east Wisconsin Avenue milwaukee, WI 53202 414.297.4900 TEL 414.271.2400 FAX foley.com CLIENT/MATTER NUMBER 065397-0148 |
May 21, 2026
Mayville Engineering Company, Inc.
135 S. 84th Street, Suite 300
Milwaukee, Wisconsin 53214
Ladies and Gentlemen:
We have acted as counsel to Mayville Engineering Company, Inc., a Wisconsin corporation (the “Company”), in connection with offering by the Company of up to 5,000,000 shares of common stock, no par value per share, of the Company (the “Shares”), pursuant to (i) the Company’s Registration Statement on Form S-3 (File No. 333-277747), filed with the Securities and Exchange Commission (the “Commission”) on March 7, 2024, as amended by that Amendment No. 1 filed with the Commission on May 8, 2024, and declared effective on May 20, 2024 (the “Registration Statement”), the base prospectus filed as part of the Registration Statement (the “Base Prospectus”), the preliminary prospectus supplement, dated May 19, 2026 (the “Preliminary Prospectus”), and the final prospectus supplement, dated May 19, 2026 (the “Final Prospectus”, together with the Base Prospectus and the Preliminary Prospectus, the “Prospectus”), and (ii) an Underwriting Agreement, dated May 19, 2026 (the “Underwriting Agreement”), among the Company and William Blair & Company, L.L.C. and Craig-Hallum Capital Group LLC, as representatives of the underwriters referenced on Schedule I thereto.
As counsel to the Company, we have examined: (i) the Underwriting Agreement, (ii) the Registration Statement and the Prospectus, (iii) the Amended and Restated Articles of Incorporation of the Company, (iv) the Bylaws of the Company, as amended, and (v) the proceedings and actions taken by the Board of Directors of the Company to authorize and approve the transactions contemplated by the Underwriting Agreement and the execution and delivery of the Underwriting Agreement. We have also considered such matters of law and of fact, including the examination of originals or copies, certified or otherwise identified to our satisfaction, of such records and documents of the Company, certificates of officers, directors and representatives of the Company, certificates of public officials, and such other documents as we have deemed appropriate as a basis for the opinions set forth below. In our examination of the above-referenced documents, we have assumed the genuineness of all electronic and manual signatures, the authenticity of all documents, certificates, and instruments submitted to us as originals and the conformity with the originals of all documents submitted to us as copies.
The opinion expressed herein is limited in all respects to the Wisconsin Business Corporation Law, and we express no opinion as to the laws of any other jurisdiction.
AUSTIN | BOSTON | BRUSSELS | CHICAGO | DALLAS | DENVER | DETROIT | HOUSTON | JACKSONVILLE | LOS ANGELES
MADISON | MEXICO CITY | MIAMI | MILWAUKEE | NASHVILLE | NEW YORK | ORLANDO | RALEIGH | SACRAMENTO |
SALT LAKE CITY | SAN DIEGO | SAN FRANCISCO | SILICON VALLEY | TALLAHASSEE | TAMPA | TOKYO | WASHINGTON, D.C.

Mayville Engineering Company, Inc.
May 21, 2026
Page 2
Based upon, subject to and limited by the foregoing, we are of the opinion that the Shares covered by the Registration Statement, when issued and paid for in the manner contemplated in the Registration Statement, Prospectus and Underwriting Agreement, will be validly issued, fully paid, and nonassessable.
This opinion is issued as of the date hereof, and we assume no obligation to supplement this opinion if any applicable law changes after the date hereof or if we become aware of any fact that might change the opinion expressed herein after the date hereof. This opinion is limited to the matters set forth herein, and no other opinion should be inferred beyond the matters expressly stated.
We consent to the filing of this opinion in accordance with the requirements of Item 601(b)(5) of Regulation S-K under the Securities Act of 1933, as amended (the “Securities Act”), as Exhibit 5.1 to the Company’s Current Report on Form 8-K to be filed with the Commission on the date hereof, to the incorporation by reference of this opinion into the Registration Statement and the Prospectus and to the references to our firm therein. In giving our consent, we do not admit that we are “experts” within the meaning of Section 11 of the Securities Act or within the category of persons whose consent is required by Section 7 of the Securities Act.
Very truly yours,
/s/ Foley & Lardner LLP
FOLEY & LARDNER LLP