Mark Twain is said to have remarked that a gold rush is a good time to be in the pick and shovel business. Investors may be able to apply that same bit of wisdom to the growing number of U.S. states that have legalized pot.

Although federal law prohibits the sale or possession of marijuana, Massachusetts last week joined the ranks of states — 18 plus Washington, D.C. — that allow its use for people suffering from chronic illnesses like cancer, HIV/AIDS, multiple sclerosis and epilepsy. In Washington and Colorado, meanwhile, voters passed an initiative to allow pot for recreational use.

How to invest in legalized Marijuana

Those changes have kickstarted a small but fast-growing medical-marijuana industry, estimated to be worth about $1.7 billion as of 2011, according to See Change Strategy, an independent financial-analysis firm that specializes in new markets. In Colorado alone, sales topped $181 million in 2010, and the business employed 4,200 state-licensed workers, says Aaron Smith, executive director of the National Cannabis Industry Association, a nonprofit trade group that campaigns for marijuana’s federal legalization.

In addition to profiting itself from growing and selling marijuana, the industry benefits a slew of other businesses, such as insurers, lawyers and agricultural-equipment firms, experts say. “Call it the ‘green rush,’” says Derek Peterson, CEO of GrowOp Technology, an online retailer of hydroponics — products used in the cultivation of indoor plants — and a subsidiary of OTC stock Terra Tech TRTC, -2.33% . “The industry is expanding, and there are all kinds of investment opportunities.”

For regular investors looking to get in on the action — and without having to actually grow or sell drugs — there are several small-cap stocks that stand to gain from marijuana’s growing acceptance. Medbox US:MDBX, an OTC stock with a $45 million market cap, for example, sells its patented dispensing machines to licensed medical-marijuana dispensaries. The machines, which dispense set doses of the drug, after verifying patients’ identities via fingerprint, could potentially be used in ordinary drugstores too, says Medbox founder Vincent Mehdizadeh. Based in Hollywood, Calif., the company already has 130 machines in the field, and it expects to install an additional 40 in the next quarter. “The smart money is trying to help with compliance and transparency,” Mehdizadeh says.

Expelled U.S. Olympian: Yeah, I ate marijuana

Of course, investing in drugs the federal government still outlaws poses enormous risks to investors, says Sam Kamin, a law professor and the director of the Constitutional Rights & Remedies Program at the University of Denver. In fact, nearly 500 of the estimated 3,000 dispensaries nationwide have either been closed by the federal government or shut down in the past year, says a spokesman for StickyGuide.com, an online directory and review site for medical marijuana dispensaries — and yet another ancillary business that’s currently seeking investors.

That said, there are many companies that appear to be betting on a change in federal law. Steep Hill is a quality-control laboratory that tests medical marijuana to see if there’s any contamination from mold, bacteria or harmful pesticides. The company, based in Oakland, Calif., is also actively seeking funding of up to $3 million. David Lampach, co-founder and president of Steep Hill, expects a federal law legalizing medical marijuana within the next decade. Cannabis Science in Colorado Springs, Colo. US:CBIS, an OTC stock with a market cap of $41 million, is developing marijuana-based medicines to help cancer and HIV/AIDS patients. “We’re at the beginning of the revolution in medicine,” says CEO Robert Melamede.

Other companies are creating a range of quirky products that allow people to use marijuana without smoking it. Medical Marijuana MJNA, -1.47% , an OTC stock with a $69 million market cap, based in San Diego, Calif., offers more than 50 ways to ingest marijuana, from Dixie Elixir soda to Dixie Chill ice-cream and a range of Dixie Edibles, like chocolate truffles and crispy rice treats.

While experts say competition in the medical-marijuana business is growing fast, they add that there are also still plenty of opportunities for entrepreneurs. For example, Troy Dayton, president and CEO of ArcView Group, an angel investor network for the industry, says demand has been growing for handheld tobacco vaporizers like those made by Ploom (which charges $250 for its “premium loose-leaf vaporizer”). “There’s a rush now to make the ideal vaporizer,” Dayton says. “There’s still room for a kingmaker in this space.”